node.js - 如何使用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;
解决方案
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 }
);
推荐阅读
- java - Java 应用程序生成一个应用程序并结束。应用程序有时也会被杀死
- sonarqube - 使用 sonarqube 对 cshtml 文件进行自定义代码分析
- reactjs - React-Query 知道数据何时发生变化?
- python - 在 Google Colab 笔记本中从 Cython 代码调用 Python 函数
- c# - 连接到 H2 数据库
- python - Python 枚举属性可以有一个空字符串吗?
- python - 按最小属性值对类实例的元组列表进行排序
- mysql - MySQL 中的子查询和 ROWNUM:SELECT * FROM (SELECT...)
- typescript - 推送管理器 - 更改订阅事件
- xml - 使用 Powershell .Net 方法将 XML 转换为 HTML - System.Xml.Xsl.XslLoadException:XSLT 编译错误