gpt4 book ai didi

javascript - Axios 方法将 item 作为 null 传递,而不是将实际值传递给我的 mongoose 路由

转载 作者:行者123 更新时间:2023-12-02 22:28:32 25 4
gpt4 key购买 nike

我的 Axios 方法似乎将 item.GroupID 作为 Null 传递,但它使用 item.id 成功在我的数据库中找到了该项目。

axios方法:

export const updateGroupID = item => dispatch => {
axios.post(`/api/users/${item.id}/update`, item.GroupID).then(
res =>
dispatch({
type: GROUPID_UPDATE,
payload: res.data
}),
console.log(item.id)
);
};

Mongoose 路线:

router.post("/:id/update", (req, res) => {
Users.findByIdAndUpdate(
req.params.id,
{
$set: { GroupID: req.body.GroupID }
},
{ new: true },
(err, user) => {
console.log("Triggered Update");
if (err) res.send(err);
else res.send("Account GroupID Updated" + " " + user);
}
);
});

我如何在 React 中调用 Axios 方法:

<button onClick={()=> {
const updatedID = {
id: this.props.user.user.UserId,
GroupID: this.state.GroupID
};
this.props.updateGroupID(updatedID);
}}
>join group</button>

最佳答案

Axios 默认情况下会将请求序列化为 JSON,但您没有传递有效的 JSON 负载,即

axios.post(`/api/users/${item.id}/update`, item.GroupID)

应该是

axios.post(`/api/users/${item.id}/update`, { GroupID: item.GroupID })

关于javascript - Axios 方法将 item 作为 null 传递,而不是将实际值传递给我的 mongoose 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58987055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com