我得到了一个错误的请求400,但我从后端完美地获得了我的数据。也只有当我使用React严格模式时才会发生。不知道发生了什么。添加了代码,我相信有人需要找到的问题
后端:
控制器:console.logging newLeaderboard给了我所期望的。
export const updateQuestionLeaderboard = async (req, res) => {
const { leaderboardId } = req.params;
const { questionIndex, playerId, playerPoints } = req.body;
let leaderboard;
try {
leaderboard = await Leaderboard.findById(leaderboardId);
leaderboard.questionLeaderboard[questionIndex].questionResultList.push({
playerId,
playerPoints,
});
const newLeaderboard = await leaderboard.save();
console.log({ newLeaderboard });
// console.log(newLeaderboard.questionLeaderboard[0].questionResultList);
res.status(201).json(newLeaderboard);
} catch (error) {
res.status(400).json({ message: error.message });
}
};
字符串
这是我的路线代码。路线:
router.patch(
"/:leaderboardId/questionleaderboard",
verifyToken,
updateQuestionLeaderboard
);
型
前端:操作:
export const updateQuestionLeaderboard =
(questionResult, id, token) => async (dispatch) => {
try {
const response = await fetch(
`http://localhost:3000/leaderboard/${id}/questionleaderboard`,
{
method: "PATCH",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(questionResult),
}
);
console.log({ questionResult });
const data = await response.json();
console.log({ data });
dispatch({ type: "UPDATE_QUESTION_LEADERBOARD", payload: data });
return data;
} catch (error) {
console.log(error);
}
};
型
我的主机页面在前端:
const updateLeaderboard = (data, id, score) => {
console.log("GIVE LEADERBOARD ID", id);
dispatch(updateQuestionLeaderboard(data, id, token))
.then((question) => {
console.log({ question });
setQuestionResult(question.questionLeaderboard[data.questionIndex]);
let leaderboardData = {
questionIndex: data.questionIndex,
playerId: data.playerId,
playerCurrentScore: score,
};
return dispatch(updateCurrentLeaderboard(leaderboardData, id, token));
})
.then((leaderboard) => {
console.log({ leaderboard });
console.log("DATA QUESTION INDEX", data.questionIndex);
setCurrentLeaderboard(
leaderboard.currentLeaderboard[data.questionIndex]
);
})
.catch((error) => {
// Handle any errors that might occur during the async operations
console.error(error);
});
};
型
的数据
1条答案
按热度按时间oo7oh9g91#
这是我的解决方案
字符串