如何使用nodejs在猫鼬中插入当前日期

时间:2018-07-18 19:16:00

标签: node.js mongoose

我正在尝试为每个用户插入当前日期,但出现这样的错误:

  

投掷者; //未处理的“错误”事件         ^

     

TypeError:callback.apply不是函数       在F:\ epaper \ sakshi \ epaper \ node_modules \ mongoose \ lib \ model.js:3954:16       在process.nextTick(F:\ epaper \ sakshi \ epaper \ node_modules \ mongoose \ lib \ query.js:2022:28)       在_combinedTickCallback(内部/进程/next_tick.js:131:7)       在process._tickDomainCallback(internal / process / next_tick.js:218:9)

有人可以帮助我吗?

型号:

const ClipsSchema = new mongoose.Schema({
   x1:Number,
   x2:Number,
   y1:Number,
   y2:Number,
   height:Number,
   width:Number
});

const UserSchema = new mongoose.Schema({
   dist_id : Number,
   path: [{body:String,date : Date}],
   date: { type: Date, default: Date.now },
   clips : [ClipsSchema]  
});

RESOLVER:

const x1 = param.x1;
         console.log(x1)
       const  x2 = param.x2;
        const y1 = param.y1;
        const y2 = param.y2;
         const height = param.height;
        const width = param.width;
        const data={"x1":x1,"x2":x2,"y1":y1,"y2":y2,"height":height,"width":width};
        console.log(data)
        var date = new Date();
        //var bongu = await Urls({date : ddd}).save()
        //console.log(bongu) 

                //const details= await Urls.findOneAndUpdate({dist_id:dist_id}, {clips:[{x1:x1,x2:x2,y1:y1,y2:y2,height:height,width:width}]});
        const details= await Urls.findOneAndUpdate({dist_id:dist_id},{$push: {clips:data} },{$set: {date : date }},{ upsert: true });
        console.log(details)
      // const update =  Urls.findOneAndUpdate({_id:details._id},{$set : {date:ddd}})
        //const sss = await Urls.find({_id : details._id})
        //console.log(sss)
        var status = {};
        status.status="User Created Successfully";
        return status;

1 个答案:

答案 0 :(得分:0)

猫鼬findOneAndUpdate accepts 4 arguments

  

[条件]«对象»

     

[更新]«对象»

     

[选项]«对象»可选,请参阅Query.prototype.setOptions()

     

[options.lean]«对象»如果属实,猫鼬将返回文档   作为纯JavaScript对象而不是猫鼬文档。看到   Query.lean()。

     

[回调]«功能»

运算符应作为单个对象传递,但它们是不同的对象,而options参数被视为第4个参数(回调)。由于它不是函数,因此会在调用时引发错误。

可能应该是:

Urls.findOneAndUpdate(
  {dist_id:dist_id},
  {$push: {clips:data}, $set: {date : date }},
  { upsert: true }
);