我正在尝试将Node.js id添加到从数据库中检索的会话。 我为此目的编辑了steam-login,但除了传递id之外的所有内容都有效。我知道为什么它不起作用。这是因为node.js中的查询是异步的。不幸的是,我不知道如何解决它,将它返回到函数中。
req.user = user;
connection.query("select * from users where steamid = "+user.steamid, function(err, row){
if(err) {
throw err;
} else {
req.user.id = row[0].id;
//And now when i trying to console.log(req.user.id) i will se the id from database.
}
});
//But here when i try console.log(req.use.id) i see undefined.
我需要从数据库中设置req.user.id = id
。我不能这样做,因为这不行。
答案 0 :(得分:0)
一旦获得查询结果,请将其返回:
user.find('first', { where: "steamid = " + player.steamid }, function(err, row) {
if (row === undefined || row === null) {
user.save();
} else {
user.set('id', row.id);
user.save();
}
});
user.find('first', { where: "steamid = " + player.steamid }, function(err, row) {
return Promise.resolve({
id: row.id,
_json: player,
steamid: steamID,
username: player.personaname,
name: player.realname,
profile: player.profileurl,
avatar: {
small: player.avatar,
medium: player.avatarmedium,
large: player.avatarfull
},
hascsgo: hascsgo
});
});