Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在插入新用户之前,尝试检查my mongodb中是否存在用户(以防止多个相同的电子邮件注册)_Javascript_Node.js_Mongodb_Express - Fatal编程技术网

Javascript 在插入新用户之前,尝试检查my mongodb中是否存在用户(以防止多个相同的电子邮件注册)

Javascript 在插入新用户之前,尝试检查my mongodb中是否存在用户(以防止多个相同的电子邮件注册),javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,使用express。我试图创建一个.js文件来处理POST请求,在插入到我的MongoDB之前检查用户是否存在。我构建了2个MongoClient连接来处理不同的情况。首先是检查用户是否存在。如果用户不存在,那么它将转到第二个连接。我不明白我做错了什么 代码是:` router.post('/',函数(req,res,next){ 日志(“内部post方法概要文件”) ssn=请求会话; ssn.firstName=req.body.fname; ssn.lastName=req.body.ln

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

代码是:`

router.post('/',函数(req,res,next){
日志(“内部post方法概要文件”)
ssn=请求会话;
ssn.firstName=req.body.fname;
ssn.lastName=req.body.lname;
ssn.userEmail=req.body.email;
ssn.userPass=req.body.pass;
让userExists=false;
连接(url,函数(err,db){
如果(错误)抛出错误;
设dbo=db.db(“projectOne”);
让myInfoLog={
电子邮件:ssn.userEmail,
pass:ssn.userPass
};
//正在尝试反检查用户是否已存在
dbo.collection(“userInfo”).findOne(myInfoLog,函数(err,数据){
如果(data.email){
userExists=true;
ssn.signUpError=“用户电子邮件已存在”;
log(“数据返回活动电子邮件”);
db.close();
res.redirect('/');
}
});
});
if(userExists==false){
连接(url,函数(err,db){
如果(错误)抛出错误;
设dbo=db.db(“projectOne”);
让myInfoLog={
电子邮件:ssn.userEmail,
pass:ssn.userPass
};
让我的信息={
fname:ssn.firstName,
lname:ssn.lastName,
电子邮件:ssn.userEmail,
pass:ssn.userPass
};
dbo.collection(“userInfo”).insertOne(myInfo,函数(err,数据){
如果(错误)抛出错误;
控制台日志(“插入收集”);
//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]。电子邮件;
ssn.userPass=data.ops[0].pass;
console.log(“欢迎!”+ssn.firstName+“”+ssn.lastName)
db.close();
});
res.redirect('/profile');
});
}
});
module.exports=路由器;
我得到一个错误:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\express\lib\response.js:767:10)
at ServerResponse.location (C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\express\lib\response.js:884:15)
at ServerResponse.redirect (C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\express\lib\response.js:922:18)
at C:\Users\rousb\Desktop\Internship2020\P_01\routes\profile.js:47:15
at executeCallback (C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\mongodb\lib\operations\execute_operation.js:70:5)
at handleCallback (C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\mongodb\lib\utils.js:128:55)
at C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\mongodb\lib\operations\find_one.js:29:9
at C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\mongodb\lib\utils.js:731:5
at C:\Users\rousb\Desktop\Internship2020\P_01\node_modules\mongodb\lib\cursor.js:251:9 {

代码:“ERR_HTTP_HEADERS_SENT”

如果(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
    }

您正在同时执行两个异步操作。它们最终都尝试
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;

您正在同时执行两个异步操作。它们最终都尝试
res.redirect(“…”);
一旦用户被重定向,您就无法重定向。您的
if(userExists==false){
在您在上面的块中执行检查之前执行感谢上面的初始注释!让我思考一下!结束代码是:我已经声明userExist为空变量,然后在(userExists==false){My code block}时执行。您让我意识到它们是异步运行的。非常感谢!