node.js – 如果我没有回调函数,Mongoose不会更新我的文档

这是我的模特:

var UserSchema = new Schema({
    username: { type: String,unique: true,index: true },url: { type: String },name: { type: String },github_id: { type: Number },avatar_url: { type: String },location: { type: String },email: { type: String },blog: { type: String },public_repos: { type: Number },public_gists: { type: Number },last_github_sync: { type: Date },created_at: { type: Date,default: Date.Now }
});

我尝试使用findOneAndUpdate函数更新我的文档:

不起作用:

User.findOneAndUpdate({ github_id: 1 },{ last_github_sync: new Date() },{});

不起作用:

User.findOneAndUpdate({ github_id: 1 },{ last_github_sync: new Date() });

作品:

User.findOneAndUpdate({ github_id: 1 },{},function (err,numberAffected,raw) { });

我的意思是我应该绑定回调函数以使更新操作工作,但我不需要那个回调函数,我的代码有什么问题?

解决方法

请参阅findOneAndUpdate documentation中的示例:

A.findOneAndUpdate(conditions,update,options,callback) // executes
A.findOneAndUpdate(conditions,options)  // returns Query
A.findOneAndUpdate(conditions,update)           // returns Query
A.findOneAndUpdate()                             // returns Query

如果您不提供回调,它将返回一个Query对象,您必须调用该对象以执行更新.

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...