【问题标题】:Unable to get document using document.findOne()无法使用 document.findOne() 获取文档
【发布时间】:2023-01-08 01:25:01
【问题描述】:

我尝试使用 document.findOne() 获取文档,但它的值显示为 undefined 。 这是我的代码 `app.post("/studentlogin",(req,res)=> {

let password;
console.log("login page");
bcrypt.hash(req.body.password,saltRounds,(err,hash)=>
{
      const user= Student.findOne({srno:req.body.srno});
      console.log(user.srno);
    if(req.body.srno==user.srno && hash==user.password)
    {
        session=req.username;
        session.userid=req.body.srno;
        res.redirect("/");
    }
    else{
        console.log("invalid user");
        res.redirect("/studentlogin");
    }
});

})`

我正在使用快速会话实现会话身份验证。在这里,当我登录用户时,它显示了模式和一堆我不知道的其他东西(错误太长)。 user.srno 也显示为未定义。我该如何解决?

我尝试使用回调函数,它正确地给了我所需的文件。但我希望查询返回正确的文档并将其存储在用户中。 使用回调函数 `app.post("/studentlogin",(req,res)=> {

let password;
console.log("login page");
bcrypt.hash(req.body.password,saltRounds,(err,hash)=>
{
      Student.findOne({srno:req.body.srno},(err,result)=>
    {
        console.log(result);
    });
    //console.log(user.srno);
    if(req.body.srno==user.srno && hash==user.password)
    {
        session=req.username;
        session.userid=req.body.srno;
        res.redirect("/");
    }
    else{
        console.log("invalid user");
        res.redirect("/studentlogin");
    }
});

})`

【问题讨论】:

  • 使用等待。 findOne 返回承诺。这是一个异步调用。第二个代码 sn-p 是正确的。

标签: node.js mongodb express mongoose session


【解决方案1】:

您面临的错误是因为您没有在代码中使用 async await 。在函数定义中使用 async 然后在搜索数据库的地方使用 await 。

const someFunction = async(req,res)=>{
// your code
 const user=await Student.findOne({srno:req.body.srno});
// your code
}

您的代码应如下所示。

app.post("/studentlogin", async(req,res)=> {
// your code
const user=await Student.findOne({srno:req.body.srno});
// your code 
}

console.log(user) 来验证。 希望能帮助到你。

【讨论】:

    【解决方案2】:

    在执行下一个任务(如比较密码)之前,您需要等待数据库查询的结果,并且看起来您只是尝试登录,您不会注册一个新的,所以最好使用比较方法在 Bcrypt 中是这样的:

    app.post("/studentlogin", async (req , res) => {
        const {srno, password} = req.body  // destructuring your request is better for visibility
    try {
        const user = await Student.findOne({srno: srno});//await the result before next step
         console.log(user.srno)  //you can check 
        if(!user) {
            console.log("invalid user");
            // your logic to tell not student found /wrong username or password, and/or redirect
        }
        const isMatch = await bcrypt.compare(password, user.password) //await the result and this method for comparing the request password and the user's password found
        
            if(!isMatch) {
                //your code to tell Wrong username or password
                res.redirect("/studentlogin");
            } else {
                // your code to access to the login.
                res.redirect("/");
            }
        } catch (err) {
            console.log(err)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多