Model.find() no longer accepts a callback in Mongoose

Dianne De JesusDianne De Jesus
1 min read

When updating Mongoose on an old practice project I got this error in reference to my passport deserializeUser. I found a reply on https://stackoverflow.com/questions/75655652/model-find-no-longer-accepts-a-callback-in-mongoose that worked well.

Original:

passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

Updated:

passport.deserializeUser(function(id, done) {
    User.findById(id).then(function(user){
        done(null, user);
    }).catch(function(err){
        done(err, null);
    })
});

Just reverted to using the promise methods.

0
Subscribe to my newsletter

Read articles from Dianne De Jesus directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dianne De Jesus
Dianne De Jesus