【问题标题】:How to pass req values into Sequelize findAll如何将 req 值传递到 Sequelize findAll
【发布时间】:2020-05-27 08:51:43
【问题描述】:

如何使用 Sequelize findAll() 从 req 传递 emailpassword 值以从数据库中查询电子邮件和密码。以下是我需要执行的选择查询;

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

发送请求时,我在 n/w 选项卡响应中遇到以下错误:

{"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();
  };

【问题讨论】:

    标签: reactjs express sequelize.js react-hooks


    【解决方案1】:

    get方法转换为post

    在节点中:

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

    在反应中:

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

    【讨论】:

    • 请问为什么我们需要添加 post 而不是 get,因为我们计划从数据库中获取数据。对不起,如果我的问题太生硬或没有任何意义..
    • 是的没问题,但是要获取数据,您需要发布一些数据对吗?要登录,您需要传递一些信息,这些信息不应作为查询参数传递,并且由于发布数据,您将获得一些数据,每当您传递一些与表单提交相关的数据时,您可以使用 post,但您可以在搜索或过滤或列出某些数据时使用 get。
    • 你知道findAll({ where: { email: req.body.email, password: req.body.password} });是对的吗?
    • Sorry Get data from database:undefined, `console.log("Get data from database:"+loginData.password);`
    • 是的,我觉得不错,首先检查你是否在节点内获取数据? ,如果是,loginData 是什么?这就是你可以调试的方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2015-10-05
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多