user3215609
user3215609

Reputation: 535

MongooseJS save additional params

I am using POST query to save data in my app. How can I add additional params to Item? I want to add something like user: req.user._id.

var Item = new Model(req.body);
    Item.save(function (err, model) {
        res.send(model);
    });

So my document will look like:

{
   param1: '1',
   param2: '2',
   user: ObjectID('blahblahblah')
}

From my form I got two params - param1 and param2. user parameter was added before saving the document.

Upvotes: 0

Views: 42

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151142

Assuming what you mean is adding something additional that is not in the form, then it is just a JavaScript object so there is nothing wrong with this:

req.body.userId = "Me";

And now req.body contains your new property that you can pass into your other constructor or even directly save. Try it in a REPL or with console.log

Upvotes: 1

Related Questions