如果没有回调函数,猫鼬不会更新我的文档 [英] Mongoose doesn't update my document if I have no callback function
本文介绍了如果没有回调函数,猫鼬不会更新我的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的模特
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
函数更新我的文档:
I try to update my document with findOneAndUpdate
function:
不起作用:
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 }, { last_github_sync: new Date() }, {}, function (err, numberAffected, raw) { });
我的意思是我应该绑定回调函数以使更新操作起作用,但是我不需要该回调函数,我的代码有什么问题?
I mean I should bind callback function to make the update operation work but I don't need that callback function, what's the problem with my code?
推荐答案
请参见findOneAndUpdate
文档:
A.findOneAndUpdate(conditions, update, options, callback) // executes
A.findOneAndUpdate(conditions, update, options) // returns Query
A.findOneAndUpdate(conditions, update, callback) // executes
A.findOneAndUpdate(conditions, update) // returns Query
A.findOneAndUpdate() // returns Query
如果不提供回调,它将返回一个Query
对象,您必须调用该对象
If you don't provide a callback, it returns a Query
object that you must call exec()
on to execute the update.
这篇关于如果没有回调函数,猫鼬不会更新我的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文