我正在创建一个具有next.js应用编程接口功能的全栈项目。以下是以下组件。当我向api发出post请求时,会出现以下错误。我已经看过了堆栈溢出上的所有类似链接
错误Parameter \"obj\" to Document() must be an object, got {\r\n \"name\": \"Charming Studio < 10 Miles to Wells' Beaches!\",\r\n \"pricePerNight\": 168,\r\n \"description\": \"A friendly atmosphere and natural delights await your visit to the town of Wells! Stay at this well-equipped 1-bath studio and enjoy easy access to several beaches, including Wells Beach and Drakes Island Beach, as well as Rachel Carson National Wildlife Refuge - the best spot for wildlife viewing just 8 miles away. Not to mention, with the downtown area just 10 mi
index.js
import nc from "next-connect";
import { allRooms, newRoom } from "../../../controllers/roomControllers";
const handler =nc();
if (mongoose.connection.readyState >= 1) {
return;
}
mongoose.connect(process.env.DB_LOCAL_URI, {
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
useNewUrlParser: true,
}).then(connect=>console.log("connected to local data base "))
handler.post(newRoom);新房间
import Room from "../models/Rooms";
export const newRoom = async (req, res) => {
try {
const room =await Room.create(req.body);
res.status(200).json({
room,
});
} catch (err) {
res.status(400).json({
error: err.message
});
}};房间模型
import mongoose from 'mongoose'
const roomSchema = new mongoose.Schema({
// all schema values
})
module.exports = mongoose.models.Room || mongoose.model('Room', roomSchema);发布于 2021-07-17 01:19:44
我认为您可以尝试使用JSON格式插入原始表单正文,而不是在Postman中插入文本。
发布于 2021-11-03 01:47:49
此错误源于您在代码中传递的不存在的内容。您应该查找此参数并将其从代码中移除或为其添加值。
https://stackoverflow.com/questions/67870581
复制相似问题