第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在插入新用戶之前嘗試檢查我的 mongodb 中是否存在用戶

在插入新用戶之前嘗試檢查我的 mongodb 中是否存在用戶

森林海 2022-05-22 11:21:54
使用快遞。我正在嘗試創(chuàng)建一個(gè) .js 文件來(lái)處理 POST 請(qǐng)求,以在插入到我的 MongoDB 之前檢查用戶是否存在。我建立了 2 個(gè) MongoClient 連接來(lái)處理不同的情況。首先是檢查用戶是否存在。如果用戶不存在,那么它將轉(zhuǎn)到第二個(gè)連接。我無(wú)法理解我做錯(cuò)了什么代碼是:`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;
查看完整描述

2 回答

?
慕田峪9158850

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個(gè)贊

您正在同時(shí)執(zhí)行 2 個(gè)異步操作。他們最終都試圖res.redirect('...');在用戶已經(jīng)被重定向后無(wú)法重定向。您if (userExists == false) {在上面的塊中進(jìn)行檢查之前執(zhí)行。


你可以像這樣鏈接你的回調(diào):


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;



查看完整回答
反對(duì) 回復(fù) 2022-05-22
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

您應(yīng)該將您的if (userExists == false) {塊移動(dòng)到第一個(gè)查找中。


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

    }


查看完整回答
反對(duì) 回復(fù) 2022-05-22
  • 2 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)