微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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对象,您必须调用该对象以执行更新.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐