【发布时间】:2021-03-06 02:44:19
【问题描述】:
我目前正在使用 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)
});
在反应中定义路线:
<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()。
我错过了什么或者这完全是错误的方法吗?如果您需要更多详细信息,请告诉我!
【问题讨论】:
-
{where:req.params.id}错误
标签: node.js reactjs express react-router sequelize.js