首页 > 解决方案 > 如何使用nodejs在猫鼬中插入当前日期

问题描述

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

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

TypeError: callback.apply is not a function at F:\epaper\sakshi\epaper\node_modules\mongoose\lib\model.js:3954:16 at process.nextTick (F:\epaper\sakshi\epaper\node_modules\mongoose\ lib\query.js:2022:28) 在 _combinedTickCallback (internal/process/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]  
});

解析器:

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;

标签: node.jsmongoose

解决方案


MongoosefindOneAndUpdate接受 4 个参数

[条件] «对象»

[更新] «对象»

[选项] «Object» 可选参见 Query.prototype.setOptions()

[options.lean] «Object» 如果为真,mongoose 会将文档作为纯 JavaScript 对象而不是 mongoose 文档返回。请参阅 Query.lean()。

[回调] «功能»

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

它可能应该是:

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

推荐阅读