Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose doesn't update my document if I have no 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 try to update my document with findOneAndUpdate function:

Doesn't work:

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

Doesn't 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 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?

like image 880
Afshin Mehrabani Avatar asked Dec 01 '22 22:12

Afshin Mehrabani


1 Answers

See the examples in the findOneAndUpdate documentation:

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 don't provide a callback, it returns a Query object that you must call exec() on to execute the update.

like image 51
JohnnyHK Avatar answered Dec 04 '22 06:12

JohnnyHK