mysql - 从数据库传递 row.id 以在函数中返回 - Node.js

标签 mysql node.js variables steam-web-api

我正在尝试将 Node.js id 添加到从数据库检索的 session 中。 我为此目的编辑了 steam 登录,但除了传递 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
我不能像up那样做,因为这不起作用。

最佳答案

得到查询结果后立即返回:

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
     });                                                           
 });

关于mysql - 从数据库传递 row.id 以在函数中返回 - Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48254882/

相关文章:

javascript - 如何将带有链接的字符串解析为html链接

c# - 如何在 C# 中创建一个变量来保存日期?

SQL 变量比静态值返回需要更长的时间

mysql - 连接多个值

mysql,字段列表中的未知列

php - 在html表中显示数据库表中的所有数据

javascript - 如何为通过 URL 传递变量创建额外的保护? [JavaScript]

mysql - 为什么不反射(reflect)docker-compose.yml的编辑内容?

javascript - 从 Node 中解析的 csv 文件构建对象数组

node.js - 无法在 Ubuntu 20.4 服务器上运行 npm install