Mongoose does not refresh my document unless I have a callback function

This is my model:

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 } }); 

I am trying to update my document using the findOneAndUpdate function:

Does not work:

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

Does not work:

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

Works:

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

I mean, I need to bind the callback function to do the update operation, but I don't need this callback function, what's the problem with my code?

+4
source share
1 answer

See the examples in the documentation for 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 

If you do not provide a callback, it returns a Query object that you must call exec() to perform the update.

+14
source

All Articles