首页 > 解决方案 > 如果用户不存在于数据库中,我想打印错误文本

问题描述

如果数据库中不存在用户,我想打印错误文本。但是我找不到,我该怎么做。我正在使用节点、快递、猫鼬、mongodb、ejs。这是我的 app.js 文件 /signin post 方法。

app.post("/Signin", (req, res) => {
    Netflix.findOne({ "email": req.body.email }, { "password": req.body.password }, function (err, user) {
        if (err) {
            res.send(err);
        }
        else {
            if (user) {
                res.redirect("/Netflix/Movies");
            }
            else {
                res.redirect("/");
            }

        }
    });
});

这是我的 Sign.ejs 文件(表单)。

 <form class="Form" action="/Signin" method="POST">

                    <h1>Sign In</h1>
                    <p id="error-text">Sorry, we can't find an account with this email address. Please try again or <a
                            href="/">create a new account</a>.</p>
                    <input id="email" type="email" placeholder="Email or phone number" name="email">
                    <input id="password" type="password" placeholder="Password" name="password">
                    <button onclick="validate()">Sign In</button><br>
                    <input type="checkbox" name="checkbox">
                    <label for="checkbox">
                        <p>Remember me</p>
                    </label>
                    <p class="New">New to Netflix? <span><a href="/">Sign up now.</a></span></p>
                </form>

这是我的 Signin.ejs 文件脚本。

script type="text/javascript">
            function validate() {

            }
        </script>

但是现在在我的 Signin.css 文件中,错误文本的可见性是隐藏的,如果在我的数据库中找不到用户,我想让它可见,我该怎么做?

标签: javascriptnode.jsmongooseejs

解决方案


我不确定是否有更好的方法,如果我错了,请纠正我,但您应该使用 javascript 在前端执行登录/登录请求。

所以假设你有你的表格:

<form class="Form" action="/Signin" method="POST">

                    <h1>Sign In</h1>
                    <p id="error-text">Sorry, we can't find an account with this email address. Please try again or <a
                            href="/">create a new account</a>.</p>
                    <input id="email" type="email" placeholder="Email or phone number" name="email">
                    <input id="password" type="password" placeholder="Password" name="password">
                    <button onclick="validate()">Sign In</button><br>
                    <input type="checkbox" name="checkbox">
                    <label for="checkbox">
                        <p>Remember me</p>
                    </label>
                    <p class="New">New to Netflix? <span><a href="/">Sign up now.</a></span></p>
                </form>

因此,您已经有了一个名为 validate() 的函数。在该验证函数中,向您的 API 请求登录。

import axios from "axios"

validate = async function() {
  try{  
    // Get credentials
    const email = // get email;
    const password = // get password;
  
    // Make request to API
    const signin = axios({
      route: "/api/signin",
      method: "// can be POST or GET depending on how you set it"
    });
  } catch(err){
    // If theres an error, set the error message to it
    const message = err.message; // Not sure if this is the correct way to get error, but 
                                    you can check in console.log()
   document.getElementById("error-text").textContent = message;
   
  }
}

注意 - 确保您的登录请求发送 res.cookie,否则它不会让您登录


推荐阅读