Node.js 向服务器发送数据时的无限循环
我想更新数据库中的数据(JSON的一条记录中只有一个字段),但我在发送数据时得到一个无限循环。我不确定问题出在哪里,所以我想向StackOverflow社区寻求帮助 因此,我在MongoDB中有一个记录:Node.js 向服务器发送数据时的无限循环,node.js,reactjs,mongodb,express,Node.js,Reactjs,Mongodb,Express,我想更新数据库中的数据(JSON的一条记录中只有一个字段),但我在发送数据时得到一个无限循环。我不确定问题出在哪里,所以我想向StackOverflow社区寻求帮助 因此,我在MongoDB中有一个记录: _id: ... some ID ... active:true nameOfEvent: .. same name .. creatorId: ...some ID ... createdAt: 2021-01-28T16:49:02.374+00:00 updatedAt: 2021-02
_id: ... some ID ...
active:true
nameOfEvent: .. same name ..
creatorId: ...some ID ...
createdAt: 2021-01-28T16:49:02.374+00:00
updatedAt: 2021-02-08T12:07:42.364+00:00
__v: 0
codeEvent: ... some code ...
participants: [Array]
对于数组参与者,我想插入一条记录(不是无穷大:-)
前端:
addResultOfUserToParticipants(eventValues._id, participant).then(data => {
if(!data.error){
setSendStatus({...sendStatus, success: true, error: false});
}
else{
setSendStatus({...sendStatus, success: false, error: data.error});
}
})
前端获取:
export const addResultOfUserToParticipants = (eventId, resultOfQuiz) => {
return fetch(`http://localhost:5000/api/event/addParticipant/${eventId}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(resultOfQuiz)
})
.then(response => {
return response.json();
})
.catch(err => console.log(err));
}
回程路线:
//add participant of event with result from quiz
router.post("/event/addParticipant/:eventId", addResultOfQuizToEventHistory);
后端:
const addResultOfQuizToEventHistory = (req, res) => {
Event.findOneAndUpdate({_id: req.event._id}, {$push: {'participants': req.body}}, {useFindAndModify: false}, (err) => {
if(err){
return res.status(400).json({
err: 'Error while inserting to participants'
});
}
else{
return res.status(200).json({
msg: 'OK'
});
}
});
};
你有什么办法解决这个问题吗?谢谢。您如何调用
addResultOfUserToParticipants
函数?您需要向我们展示如何触发API调用谢谢您的想法,我编辑了主要帖子谢谢您指出我的问题!这是我的问题。在前端,我没有正确关闭执行函数addResultToUserToParticipants。我编辑了if,else语句,现在它工作了!谢谢。不客气。:)