我是React新手,遇到了一些问题。
这是我当前的API调用。
Axios.post(Addr_currentRound_addOne, { gameId: gameId }).then(history.push("/leiter_tunierplan/"+gameId));
这是相应的API代码片段。
app.post("/currentRoundAddOne", (req, res) => {
const gameId = req.body.gameId;
db.query(
"UPDATE tunierplan SET currentRound=currentRound+1 WHERE id=?;",
[gameId],
(err, result) => {
if (err) {
console.log(err);
} else {
res.send("Daten Übertragen");
}
}
);
});
这里的问题:当调用执行时,currentRound应该总是增加+1。然后重定向到给定的"/leiter_tunerplan/"但有时,它不起作用。它没有增加任何值(+1)我的第一个想法是:因为它是异步的,我从历史中被取消了。推,在它完成之前。我说的对吗?我该怎么补救呢?谢谢已经。
游戏gameId
是否已经为下一页增加了id ?我没有看到它在你的代码中被增加。
如果是,请尝试放置历史记录。推入回调,如下所示:
...then(() => history.push("/leiter_tunierplan/"+gameId));
这是因为then
接受回调函数作为参数
解决方案是添加preventDefault();
我的问题是,侧面被重新加载…
我认为您尝试async
-await
,因为对数据库的请求是基于承诺的。我的建议:
app.post("/currentRoundAddOne", async (req, res) => {
const gameId = req.body.gameId
const data = await db.query("UPDATE tunierplan SET currentRound=currentRound+1 WHERE id=?;", [gameId],
(err, result) => {
if (err) console.log(err);
}
console.log(data) // check updated data, it should return id
})
我希望它能工作!