代码应按以下方式工作:如果目标用户名和登录的用户名在同一斗争中,则不应创建斗争,而应将用户重定向到/arena
。但是问题是,即使我完成战斗然后快速按F5键,也将再次创建具有相同用户和相同目标的新战斗。什么会导致此问题?
app.get('/arena/fight/user/:username', async (req, res) => {
User.findById(req.session.userId).then((user) => {
if(req.params.username === user.username) {
return res.redirect('/arena');
}
});
const target = await User.findOne({username: req.params.username});
const user = await User.findById(req.session.userId);
const fight = await TempFight.findOne({user: user.username});
if(fight && target.username === fight.target) {
return res.redirect('/arena');
} else {
const battleId = uuid4();
const newtempFight = TempFight({
target: target.username,
user: user.username,
code: battleId
});
await newtempFight.save();
}
if(req.session.userId) {
res.render(__dirname + '/views/arena/battle', {messages, user, enemy, userItems, enemyItems, liveitems, fights, lostFights});
} else {
return res.redirect('/');
}
});
答案 0 :(得分:0)
尝试一下
%{:key => val}