Auclown
Auclown

Reputation: 308

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.

Upvotes: 6

Views: 25103

Answers (5)

Kubadev
Kubadev

Reputation: 865

I know I dig up this question, but I think you forget to send your POST request in JSON format.

Upvotes: 3

David
David

Reputation: 71

Put the name and number as an object like this let contact = new ContactData({name, number});

Upvotes: 5

Robin Singh
Robin Singh

Reputation: 157

Your Error ObjectParameterError: Parameter "obj" to Document() must be an object, got John Doe

It says all you are not passing it an object.

Edit this line in your code

let contact = new ContactData(name, number);

Pass your values as an object like this below:

let contact = new ContactData({name, number});

Upvotes: 4

Matin Sasan
Matin Sasan

Reputation: 1885

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.

Upvotes: 13

Amol B Jamkar
Amol B Jamkar

Reputation: 1257

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

Try this,

let contact = new ContactData({name, number});

Upvotes: 8

Related Questions