如何使用Sequelize findAll()
从 req 传递email
和password
值,以查询数据库中的电子邮件和密码。下面是我需要执行的选择查询;
select email, password from user where email="some_email@testmail.com";
发送请求时,我在 n/w 选项卡响应中出现以下错误:
{"消息":"WHERE 参数 \"电子邮件\" 具有无效的 \"未定义\" 值"}
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();
};
将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 });