问题描述
我正在尝试查看来自 MysqL 的数据并将其传递到个人资料页面,在那里我可以看到用户的信息,但我尝试了很多次和不同的方法,但找不到方法,而且我使用的也没有错误{ {#each}}{{this.fullname}}{{/each}} 我也累了,但数据没有显示,有人能帮我解决这个问题,或者是否有另一种方式来显示我的数据
我正在使用 node js、express 和 hbs 引擎
router.get('/profilePage/:userid',function(req,res) {
var userid = req.params.userid;
pool.query('SELECT * from users where id = ?',userid,function(error,results,feilds) {
if (error) {
console.log("error ocurred while getting user details of " + userid,error);
res.send({
"code": 400,"Failed": "error ocurred"
});
} else {
res.render("profilePage",{user:results});
}
});
});
解决方法
试试这个:
router.get('/profilePage/:userid',function(req,res) {
var userid = req.params.userid;
if (!userid) {
//No user id provided
} else {
pool.query('SELECT * from users where id = ?',[userid],(error,results,feilds)=> {
if (error) {
console.log("error ocurred while getting user details of " + userid,error);
res.send({
"code": 400,"failed": "error ocurred"
});
} else {
if (results.length > 0) {
const profile = results[0]
//You can then access the user id with
console.log(profile.id)
res.render("profilePage",{user:profile});
} else {
console.log('unable to retrieve a profile')
}
}
});
}
});