Skip to content
Advertisement

MongoDB returning an onject that doesnt exist

I was working on a project with MongoDB and passport, when i ran into this error, event though p1 isn’t used, it still reruns an object im guessing, because it just says that the field p1 is taken, when it isn’t. The same is happening with p2. Does anyone know why ?

passport.use(
  "local.signup",
  new LocalStrtegy(
    {
      usernameField: "email",
      passwordField: "password",
      passReqToCallback: true,
    },
    async function (req, email, password, done) {
      req.checkBody("email", "E-mail is empty").notEmpty();
      req
        .checkBody("password", "Your password is too short!")
        .isLength({ min: 4 });
      var errors = await req.validationErrors();
      if (errors) {
        var messages = [];
        errors.forEach(function (error) {
          messages.push(error.msg);
        });
        return done(null, false, req.flash("error", messages));
      }
      const p1 = User.find({ p1: req.body.p1 });
      const p2 = User.find({ p2: req.body.p2 });

      User.findOne({ email: email }, function (err, user) {
        if (err) {
          return done(err);
        }

        if (user) {
          return done(null, false, {
            message:
              "This E-Mail alredy in use! If you believe that this is an error, please an admin on. (ERR 002 MCEE)",
          });
        } else if (p1) {
          return done(null, false, {
            message:
              "This username is alredy in use! If you believe that this is an error, please contact an admin. (ERR 002 MCEM)",
          });
        } else if (p2) {
          return done(null, false, {
            message:
              "This Tag is alredy in use! If you believe that this is an error, please contact an admin. (ERR 002 MCED)",
          });
        }

        console.log(mc + " " + dcign + " " + user);
        var newUser = new User();
        newUser.email = email;
        newUser.password = newUser.encryptPassword(req.body.password);
        newUser.p1 = req.body.p1;
        newUser.p2 = req.body.p2;
        newUser.Banned = false;
        console.log(req.body);
        newUser.save(function (err, result) {
          if (err) {
            return done(err);
          }
          return done(null, newUser);
        });
      });
    }
  )
);

Advertisement

Answer

Calling User.find returns a Promise which you are not awaiting. So when you are checking for existence of p1 and p2, it returns a truthy value as both values are Promise objects.

To fix the issue use await in front of both User.find like this

const p1 = await User.find({ p1: req.body.p1 });
const p2 = await User.find({ p2: req.body.p2 });

After that both values will be array as you are using find method so just check for length property or better use findOne instead of find method.

const p1 = await User.findOne({ p1: req.body.p1 });
const p2 = await User.findOne({ p2: req.body.p2 });
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement