当我获取一个对象添加到我的 mongodb 时
const response = await fetch("/api/contact", {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
});
我在这里得到[出了问题 TypeError: res.json 不是函数]
import React from "react";
import { NextResponse, NextRequest } from "next/server";
import wordModel from "../../../models/wordModel";
import mongoose from "mongoose";
export async function POST(req, res) {
const MONGO_URI = process.env.MONGO_URI;
try{
await mongoose.connect(MONGO_URI)
console.log("It's good");
const word = await wordModel.create(req.body);
res.json({ word });
} catch(error) {
console.log("Something went wrong", error);
return new Response;
}
}
然后在我的数据库中出现一个包含随机数字和字母的文档
参见:https://nextjs.org/docs/app/api-reference/functions/next-response#json
你不应该这样吗?
export async function POST(req, res) {
const MONGO_URI = process.env.MONGO_URI;
try{
await mongoose.connect(MONGO_URI)
console.log("It's good");
const word = await wordModel.create(req.body);
return NextResponse.json({ someProp: word }, { status: 200 })
} catch(error) {
console.log("Something went wrong", error);
return NextResponse.json({ error: "Something went wrong" }, { status: 500 })
}
}