简体   繁体   English

如何将 req 值传递到 Sequelize findAll

[英]How to pass req values into Sequelize findAll

How to pass email and password values from req using Sequelize findAll() to query the email and password from the database.如何使用Sequelize findAll()从 req 传递emailpassword值以从数据库中查询 email 和密码。 Below is my select query that I need to perform;下面是我需要执行的 select 查询;

select email, password from user where email="some_email@testmail.com";

While sending the request, I am getting below error in the n/w tab response:发送请求时,我在 n/w 选项卡响应中遇到以下错误:

{"message":"WHERE parameter \"email\" has invalid \"undefined\" value"} {"message":"WHERE 参数 \"email\" 具有无效的 \"undefined\" 值"}

server.js

app.get('/service/login', async (req, res) => {
  try {
    const loginData = await UserModel.findAll({ where: { email: req.body.email, password: req.body.password} });
    console.log(loginData);
    res.status(200).json({ loginData });
  } catch (e) {
    res.status(500).json({ message: e.message });
  }
});

login.js

 const handleSubmit = (e) => {
    e.preventDefault()
    const fetchData = async () => {
      try {
        const res = await Axios.get('http://localhost:8000/service/login');
        setLoginData(res.data.loginData); 
        console.log("Get data from database:"+loginData.password);
      } catch (e) {
        console.log(e);
      }
    }
    fetchData();
  };

Convert get method to postget方法转换为post

In Node:在节点中:

app.post('/service/login'
// then you will be able to access `req.body`

In React:在反应:

// you were not even passing the email and password
await Axios.post('http://localhost:8000/service/login',{ email , password });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM