Passing model parameters into a mongoose model

前端 未结 3 1325
走了就别回头了
走了就别回头了 2020-12-18 01:26

I have a mongoose model that has an association with a user model e.g.

var exampleSchema = mongoose.Schema({
   name: String,
   
           


        
3条回答
  •  春和景丽
    2020-12-18 02:19

    _ = require("underscore")
    
    var model = new Example(_.extend({ userId: req.user._id }, req.body))
    

    or if you want to copy userId into req.body:

    var model = new Example(_.extend(req.body, { userId: req.user._id }))
    

提交回复
热议问题