Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving data into MongoDb returns an ObjectParameterError

I am practicing on saving data into MongoDb using Node.js.

I have set up Express server and created a Mongoose model but when I try to save some data through Postman it keeps returning an ObjectParameterError.

Mongoose version is 5.5.13 and Express version is 4.17.1.

Connecting to MongoDb through Mongoose has no problem since it does not return any errors at least, but the problem is when I am trying to save some data with a post request.

Here is my post request from my server.js:

router.post("/create_contact", (req, res) => {
  const { name, number } = req.body;

  let contact = new ContactData(name, number);

  contact.save((error, contact) => {
    if (error) {
      return console.error(error);
    }

    return res.json(contact);
  });
});

And my data.js that defines the Mongoose Schema and model:

const mongoose = require("mongoose");

const ContactSchema = mongoose.Schema({
  name: String,
  number: Number
});

module.exports = mongoose.model("ContactData", ContactSchema);

As you can see, it only has 2 types of data in it: name and number.

I want to save this simple contact data into local MongoDb but I am getting this one Error:

ObjectParameterError: Parameter "obj" to Document() must be an object, got John Doe
    at new ObjectParameterError (D:\LearnReact\db\backend\node_modules\mongoose\lib\error\objectParameter.js:25:11)
    at model.Document (D:\LearnReact\db\backend\node_modules\mongoose\lib\document.js:73:11)
    at model.Model (D:\LearnReact\db\backend\node_modules\mongoose\lib\model.js:96:12)
    at new model (D:\LearnReact\db\backend\node_modules\mongoose\lib\model.js:4580:15)
    at router.post (D:\LearnReact\db\backend\server.js:28:17)
    at Layer.handle [as handle_request] (D:\LearnReact\db\backend\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\LearnReact\db\backend\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\LearnReact\db\backend\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\LearnReact\db\backend\node_modules\express\lib\router\layer.js:95:5)
    at D:\LearnReact\db\backend\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (D:\LearnReact\db\backend\node_modules\express\lib\router\index.js:335:12)
    at next (D:\LearnReact\db\backend\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\LearnReact\db\backend\node_modules\express\lib\router\index.js:174:3)
    at router (D:\LearnReact\db\backend\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (D:\LearnReact\db\backend\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\LearnReact\db\backend\node_modules\express\lib\router\index.js:317:13)

What am I missing? Thanks in advance for your help.

like image 563
Auclown Avatar asked Aug 22 '26 20:08

Auclown


2 Answers

In:

const ContactSchema = mongoose.Schema({
  name: String,
  number: Number
});

You forgot to add new that is: new mongoose.Schema({

And

{ ObjectParameterError: Parameter "obj" to Document() must be an object Mostly caused by parameters passed to mongoose that is NOT an object.

in let contact = new ContactData(name, number) , modify from (name,number) to ({name:name, number:number}) or just ({name, number}) as they have the same name.

like image 90
Matin Sasan Avatar answered Aug 25 '26 11:08

Matin Sasan


You are passing parameteres, where you need to send object to save in mongo db

Try this,

let contact = new ContactData({name, number});
like image 25
Amol B Jamkar Avatar answered Aug 25 '26 11:08

Amol B Jamkar