我有一个 post 请求,它重定向到一个新的路由,如你所见
//POST login
app.post('/login', urlencodedParser, function(req,res){
userLogin(req.body, function(response){
if(response == true){
//Activate player login in db
loginPlayerDB(req.body.username);
getPlayerId(req.body.username, function(id){
res.redirect(req.baseUrl + '/:id/profile');
});
} else{
res.send(500, "Username or Password is incorrect");
}
})
});
这个函数被调用
function getPlayerId(username, callback){
let sql = "SELECT PlayerId FROM users WHERE Username = (?)";
var values = [username];
db.query(sql, values, function(err, result, fields){
if(err){
console.log(err);
} else{
return callback(result);
}
})
}
它重定向到这条路线
app.get('/:id/profile', function(req,res){
res.render('profile/profile');
})
一切都很好,除了新页面上的 URL 是
http://localhost:3000/:id/profile
当它应该是这样的
http://localhost:3000/6/profile
如果玩家的 id 为 6。我该如何解决这个问题
//解决方案。
感谢 MadWard 的帮助。我的错误是在我的 getPlayerId 函数中,我返回结果,它是一个数组,而不是 result[0].PlayerId 以获得我正在寻找的特定 id
您实际上是重定向到/:id/profile
. 这是一个字符串,它是固定的,并且始终是“/:id/profile”。
你想要做的是,使用模板字符串:
getPlayerId(req.body.username, function(id){
res.redirect(`${req.baseUrl}/${id}/profile`);
});
像您一样使用普通字符串连接:
getPlayerId(req.body.username, function(id){
res.redirect(req.baseUrl + '/' + id + '/profile');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句