MongooseError: Model.findOne() no longer accepts a callback i can ues any help please

Model.findOne() no longer accepts a callback

exports.postLogin = (req, res, next) => {
  const validationErrors = [];
  if (!validator.isEmail(req.body.email))
    validationErrors.push({ msg: "Please enter a valid email address." });
  if (validator.isEmpty(req.body.password))
    validationErrors.push({ msg: "Password cannot be blank." });

  if (validationErrors.length) {
    req.flash("errors", validationErrors);
    return res.redirect("/login");
  }
  req.body.email = validator.normalizeEmail(req.body.email, {
    gmail_remove_dots: false,
  });

  passport.authenticate("local", (err, user, info) => {
    if (err) {
      return next(err);
    }
    if (!user) {
      req.flash("errors", info);
      return res.redirect("/login");
    }
    req.logIn(user, (err) => {
      if (err) {
        return next(err);
      }
      req.flash("success", { msg: "Success! You are logged in." });
      res.redirect(req.session.returnTo || "/profile");
    });
  })(req, res, next);
};

Hey @Yusuf_Tamale,

Thank you for reaching out to the MongoDB Community forums.

The use of callback functions has been deprecated in the latest version of Mongoose (version 7.x).

Reference: Mongoose v8.2.2: Migrating to Mongoose 7

If you are using Mongoose 7.x+, please modify the functions that use a callback by switching to the Promise or async/await syntax.

I hope this helps! If you have any further questions or concerns, feel free to ask.

Regards,
Kushagra

1 Like

This led to great losses of money and time in my business after updating the library, since all the code was with Promises and cannot be compatible and its modification in many code sheets was a misfortune for me.