mysql - 如何修复 PUT,错误 500 id = undefined ReactJs NodeJs
问题描述
我想解释一下我今天遇到的问题。
错误 500,id = undefined,不知道为什么,我尝试发布您可能需要的任何内容
我该如何解决这个问题?
这就是功能
handleSubmit = (e, id) => {
e.preventDefault();
const userIdData = { id };
const config = {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({userIdData, livree: new Date().toISOString().slice(11, 16)}),
};
const url = entrypoint + "/alluserpls";
fetch(url, config)
.then(res => res.json())
.then(res => {
if (res.error) {
alert(res.error);
else {
alert(`ajouté avec l'ID ${res}!`);
}
}).catch(e => {
console.error(e);
}).finally(() => this.setState({ redirect: true })); }
我的路线
app.put('/api/alluserpls', (req, res, ) => {
const formData = req.body;
const userId = req.body.id;
const deleteQuery = `UPDATE alluserpls SET ? WHERE id = ${userId}`;
connection.query(deleteQuery, err => {
if (err) {
console.log(err)
res.status(500).send("Erreur lors de la modification des users");
} else {
res.sendStatus(200);
}
});
});
我的按钮
<form onSubmit={(e) => this.handleSubmit(e, datass.id)}>
<button type="submit">PUT</button>
</form>
console.log 结果
errno: 1064,
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use near '? WHERE id = undefined' at
line 1",
sqlState: '42000',
index: 0,
sql: 'UPDATE alluserpls SET ? WHERE id = undefined'
}
PUT /api/alluserpls 500 14.354 ms - 40
解决方案
将 userId 行替换为:
const userId = req.body.userIdData.id;
使用 ES6 对象解构:
const { id } = req.body.userIdData
此外,如果您删除声明,它会更干净,formData
因为它没有在路由处理程序中使用。
推荐阅读
- r - 关于 R 中除法运算符不返回正确值的问题
- visual-studio - Xamarin.Forms 共享项目 - 无法解决程序集
- javascript - 如何将新的/更新的 innerHTML 附加到没有类或 id 的 ap 标记?
- html - 如何切换/动画通过将鼠标悬停在S
- kubernetes - 使用主机网络的 Kubernetes pod 是否分配了一个可以使用服务资源访问的 pod IP?
- python - 查找最接近给定值的值并返回行索引
- firebase - 访问 HackerNews API
- c - realloc() 如何重新分配内存?
- pandas - 将数据框与另一个具有单行但相同列的数据框划分
- javascript - 计算新数组映射中的项目