我目前正在使用 nodeJS、expressJS 和 reactJS。我目前可以通过单击编辑按钮访问每个数据的 ID,但是每次单击该按钮时实际上更新数据时都会遇到问题,我在控制台中收到 404 错误。我浏览了多个问题和教程,但没有一个真正帮助我解决困惑。我的 server.js 中有一个 put 路由来根据 id 更新记录:
app.put("update-list/:id", function (req, res, next) {
Information.update({
Name: req.body.name,
Phone: req.body.phone,
Email: req.body.email,
}, { where: req.params.id }
).then(function (rowsUpdated) {
res.json(rowsUpdated);
}).catch(next)
});
react 中定义的路线:<Route path="/edit/:id" component={Edit}/>
我如何获得api: let [state, setState] = useState({
name: "",
phone: "",
email: "",
});
let body = state;
useEffect(()=>{
handleSubmit(props);
})
function handleSubmit(props) {
const id=props.match.id
axios({
method: "put",
url: "http://localhost:5000/update-list/"+id,
data: body,
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
console.log(state);
}
关于更改功能:function handleChange(e, field) {
e.preventDefault();
const value = e.target.value;
setState({
...state,
[field]: e.target.value,
});
}
单击按钮调用 handleChange()
函数,表单调用 handleSubmit()
。我错过了什么还是这完全是错误的方法?如果您需要更多详细信息,请告诉我!
最佳答案
app.put("/update-list/:id", function (req, res, next) {
db.Information.update({
Name: req.body.name,
Phone: req.body.phone,
Email: req.body.email,
}, { where: { id: req.params.id } } // This is fixed
).then(function (rowsUpdated) {
res.json(rowsUpdated);
}).catch(next)
});
React.js 项目:https://github.com/nkhs/node-sequelize-react.js-client/tree/stack-66271022API 项目:https://github.com/nkhs/node-sequelize/tree/stack-66271022
关于node.js - 使用 sequelize 根据 id 更新行的值会产生 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66271022/