• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

我的JavaScript变量没有在Mongoose的.findOne()函数中设置

JavaScript 来源:Hemakshi Sachdev 4次浏览

好的,所以我使用Node.js,ExpressJS和MongoDB来制作网站。我正在尝试通过AJAX调用来检查表单的有效性(因为我想显示输入值是否在用户输入时随时可用)。我已将所有这些达到了90%。唯一不起作用的是检查用户名和电子邮件是否已经存在或不在数据库中。我的查询是完美的,但是当我试图存储查询给出的结果(无论是否在数据库中)时,它不会发生。 这里是我的代码: -我的JavaScript变量没有在Mongoose的.findOne()函数中设置

router.post('/signup',function(req, res){ 
    const username = req.body.username; 
    const email = req.body.email; 

    var response = []; 
    var usernameTaken, emailTaken; 

    User.findOne({username:username}, function(err, user){ 
    if (err) throw err; 
    if (Boolean(user)) { 
     console.log(user); // This works perfectly 
     usernameTaken = true; // This does not works at all! 
     console.log(usernameTaken); // Nor does this one. :/ 
    } else { 
     usernameTaken = false; 
    } 
    }); 

    console.log(usernameTaken); // No output 

    // Else part gets executed because usernameTaken does not sets to true. 
    if (usernameTaken) { 
    response.push({field: "username", type: "danger", message: "Sorry! This username is already taken :("}); 
    } else { 
    response.push({field: "username", type: "success", message: "This username is now yours ;)"}); 
    } 

    if (!isValid(email)) { 
    response.push({field: "email", type: "danger", message: "Please enter a valid E-mail Address."}); 
    } else { 
    response.push({field: "email", type: "success", message: "E-mail is valid."}); 
    } 

    User.findOne({email:email}, function(err, user){ 
    if (err) throw err; 
    if (Boolean(user)) { 
     console.log(user); // Again this works. 
     emailTaken = true; // But this does not works :(
    } else { 
     emailTaken = false; 
    } 
    }); 

    // Else part gets executed because emailTaken does not sets to true. 
    if (emailTaken) { 
    response.push({field: "email-avail", type: "danger", message: "This E-mail address is already registered!"}); 
    } else { 
    response.push({field: "email-avail", type: "success", message: "E-mail is valid."}); 
    } 

    res.send(response); 
}); 


===========解决方案如下:

记住,像findOne或任何功能都异步地执行这意味着你把回调里面不断的执行步骤,他们将随时得到执行。这就是为什么你的布尔值不在回调之外按预期打印的原因。

现在,您可以使用的一种方法是使用async/await,这将基本上确保您的代码在承诺解决之前停止执行。所以,以一种使整个过程同步的方式。

ASYNC

AWAIT

试试这个。我使用async/await使过程同步。

router.post('/signup',async function(req, res){ 
    const username = req.body.username; 
    const email = req.body.email; 

    var response = []; 
    var usernameTaken, emailTaken; 

    const userNamePromise = new Promise((resolve, reject) => { 
    User.findOne({username:username}, function(err, user){ 
     if (err) reject(err); 
     if (Boolean(user)) { 
     resolve(true) 
     } else { 
     resolve(false); 
     } 
    }); 
    }) 

    usernameTaken = await userNamePromise; 

    console.log(usernameTaken); 

    if (usernameTaken) { 
    response.push({field: "username", type: "danger", message: "Sorry! This username is already taken :("}); 
    } else { 
    response.push({field: "username", type: "success", message: "This username is now yours ;)"}); 
    } 

    if (!isValid(email)) { 
    response.push({field: "email", type: "danger", message: "Please enter a valid E-mail Address."}); 
    } else { 
    response.push({field: "email", type: "success", message: "E-mail is valid."}); 
    } 

    const emailPromise = new Promise((resolve, reject) => { 
    User.findOne({email:email}, function(err, user){ 
     if (err) reject(err); 
     if (Boolean(user)) { 
     console.log(user); 
     resolve(true); 
     } else { 
     resolve(false); 
     } 
    }); 
    }) 
    emailTaken = await emailPromise; 

    if (emailTaken) { 
    response.push({field: "email-avail", type: "danger", message: "This E-mail address is already registered!"}); 
    } else { 
    response.push({field: "email-avail", type: "success", message: "E-mail is valid."}); 
    } 

    res.send(response); 
}); 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)