在插入新用户之前尝试检查我的 mongodb 中是否存在用户

使用快递。我正在尝试创建一个 .js 文件来处理 POST 请求,以在插入到我的 MongoDB 之前检查用户是否存在。我建立了 2 个 MongoClient 连接来处理不同的情况。首先是检查用户是否存在。如果用户不存在,那么它将转到第二个连接。我无法理解我做错了什么


代码是:`


router.post('/', function(req, res, next) {

  console.log("inside post method profile")

  ssn = req.session;

  ssn.firstName = req.body.fname;

  ssn.lastName = req.body.lname;

  ssn.userEmail = req.body.email;

  ssn.userPass = req.body.pass;

  let userExists = false;


  MongoClient.connect(url, function(err, db) {

    if (err) throw err;

    let dbo = db.db("projectOne");

    let myInfoLog = {

      email: ssn.userEmail,

      pass: ssn.userPass

    };


    // TRYING TO COUNTERCHECK IF A USER ALREADY EXISTS

    dbo.collection("userInfo").findOne(myInfoLog, function(err, data) {

      if (data.email) {

        userExists = true;

        ssn.signUpError = "User email already exists";

        console.log("data returns an active email");

        db.close();

        res.redirect('/');

      }

    });

  });


  if (userExists == false) {

    MongoClient.connect(url, function(err, db) {

      if (err) throw err;

      let dbo = db.db("projectOne");

      let myInfoLog = {

        email: ssn.userEmail,

        pass: ssn.userPass

      };


      let myInfo = {

        fname: ssn.firstName,

        lname: ssn.lastName,

        email: ssn.userEmail,

        pass: ssn.userPass

      };

      dbo.collection("userInfo").insertOne(myInfo, function(err, data) {

        if (err) throw err;

        console.log("collection inserted");

        // console.log(data.ops[0].fname);

        // console.log(data.ops[0].lname);

        // console.log(data.ops[0].email);

        // console.log(data.ops[0].pass);


        ssn.firstName = data.ops[0].fname;

        ssn.lastName = data.ops[0].lname;

        ssn.userEmail = data.ops[0].email;

        ssn.userPass = data.ops[0].pass;

        console.log("welcome! " + ssn.firstName + " " + ssn.lastName)


        db.close();

      });


      res.redirect('/profile');


    });

  }


});


module.exports = router;


森林海
浏览 177回答 2
2回答

慕田峪9158850

您正在同时执行 2 个异步操作。他们最终都试图res.redirect('...');在用户已经被重定向后无法重定向。您if (userExists == false) {在上面的块中进行检查之前执行。你可以像这样链接你的回调:router.post('/', function(req, res, next) {  ssn = req.session;  ssn.firstName = req.body.fname;  ssn.lastName = req.body.lname;  ssn.userEmail = req.body.email;  ssn.userPass = req.body.pass;  MongoClient.connect(url, function(err, db) {    if (err) throw err;    let dbo = db.db("projectOne");    let myInfoLog = {      email: ssn.userEmail // Don't include the password!    };    // Check if email exists    dbo.collection("userInfo").findOne(myInfoLog, function(err, data) {      if (data.email) {        ssn.signUpError = "User email already exists";        console.log("data returns an active email");        db.close();        res.redirect('/');      } else {        const myInfo = {          fname: ssn.firstName,          lname: ssn.lastName,          email: ssn.userEmail,          pass: ssn.userPass        };        // Insert user        dbo.collection("userInfo").insertOne(myInfo, function(err, data) {          if (err) throw err;          console.log("collection inserted");          ssn.firstName = data.ops[0].fname;          ssn.lastName = data.ops[0].lname;          ssn.userEmail = data.ops[0].email;          ssn.userPass = data.ops[0].pass;          console.log("welcome! " + ssn.firstName + " " + ssn.lastName);          db.close();          res.redirect('/profile');        });      }    });  });});module.exports = router;

慕斯王

您应该将您的if (userExists == false) {块移动到第一个查找中。dbo  .collection("userInfo")  .findOne(myInfoLog, function(err, data) {    if (data.email) {      // handle case where user already exists    } else {      // handle case where user doesn't exist yet    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript