Parameter \"obj\" to Document() must be an object

Viewed 1291

I am creating a full stack project with next.js api feature . Here are the following components. when i make post request to the api then following error comes . and i have gone through all the similar links on stack overflow

error 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);

new room

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
    });
  }};

room model

import mongoose from 'mongoose'
const roomSchema = new mongoose.Schema({
   // all schema values 
})

module.exports = mongoose.models.Room || mongoose.model('Room', roomSchema);
2 Answers

I think you can try inserting raw form body with JSON format instead of text in Postman.

This error is originated from something non-existent that you are passing inside something in your code. You should look for this parameter and remove it from your code or add a value for it.

Related