Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mongoose stopped accepting callbacks for some of its functions

I’ve been using callbacks for .save() and .findOne() for a few days now and just today I encounter these errors:

throw new MongooseError('Model.prototype.save() no longer accepts a callback')

MongooseError: Model.prototype.save() no longer accepts a callback

and

MongooseError: Model.findOne() no longer accepts a callback

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s really awkward given that callbacks are still accepted in the docs at least for .findOne().

app.post("/register", (req, res) => {
    const newUser = new User({
        email: req.body.username,
        password: req.body.password
    });

    newUser.save((err) => {
        if (err) console.log(err)
        else res.render("secrets");
    });
});

This is what used to work for me, using express and mongoose. Please let me know how to fix it.

>Solution :

app.post("/login",function(req,res){
    const username = req.body.username;
    const password = req.body.password;

    User.findOne({email:username})
    .then((foundUser) => {
        if(foundUser){
            if(foundUser.password === password){
                res.render("secrets");
            }
        }
   })
   .catch((error) => {
       //When there are errors We handle them here

console.log(err);
       res.send(400, "Bad Request");
   });
      
});

This works for me, also im doing the web dev bootcamp by angela yu.

It seems like now you have to use => then and =>catch for the handling

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading