mysql - 使用 Node.js 和 MySQL 调用 RowDataPacket 内的对象

标签 mysql node.js node-mysql

The code its about:

con.query(`SET @row_num = 0; SELECT @row_num := @row_num + 1 AS row_number, userid, username, lvl FROM users ORDER BY lvl + 0 DESC`, async (err, rowss) => {
    if(err) throw err;
        console.log(rowss[0].userid);
    });

What i tried:

我尝试使用rowss[0].useridrowss.userid。但他不想为我工作。但是,当我单独使用 rowss 时,我得到以下输出:

[ OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 10,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 },
[ RowDataPacket {
  row_number: 1,
  userid: '123',
  username: 'test1#5245',
  lvl: '113' },
RowDataPacket {
  row_number: 2,
  userid: '456',
  username: 'test2#8272',
  lvl: '112' },
RowDataPacket {
  row_number: 3,
  userid: '789',
  username: 'test3#5873',
  lvl: '91' },
RowDataPacket {
  row_number: 4,
  userid: '012',
  username: 'test4#0581',
  lvl: '78' }.................

What i want:

在 RowDataPacket 中调用一个对象并使用 console.log 记录它。 所以我想要的基本上是当我打字时:

console.log(rowss[0].username + ' - ' + rowss[0].lvl);
console.log(rowss[3].username + ' - ' + rowss[3].lvl);

输出结果为:

test1#5245 - 113 (Since this is the first row in the array).
test4#0581 - 78 (Since this is the 4th row).

My Problem:

例如,每当我尝试 console.log(rowss[0].username); 时,我都会得到 undefined。这适用于所有对象。所以还有 userid、lvl 等。

最佳答案

您在创建数据库连接时启用了多个语句为 true。这会导致多个结果集

multipleStatements: true

下面的结果集是语句 SET @row_num = 0; 的响应

OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 10,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 }

选择查询的数据结果位于数组的第二个位置
选择可以访问的查询数据如下

rowss[1][0].row_number
rowss[1][0].userid
rowss[1][0].lvl

关于mysql - 使用 Node.js 和 MySQL 调用 RowDataPacket 内的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51852464/

相关文章:

node.js - NodeJs Azure Function App 的异步初始化

mysql - NodeJS不会使用node-mysql连接到本地数据库

node.js - 为什么我的 Q promise 不起作用?

mysql - 尽管列数正确,但行上的值计数错误

mysql - 我不能在 mysql 查询中使用 'time' 字?

javascript - 使用 php 将 append javascript 中的值 POST 提交到 mysql

node.js - 如何解决 "customFds not yet supported"错误

javascript - {app, BrowserWindow} 在 JavaScript (node.js) 中是什么意思?

mysql - 如何借助mysql中的sql事务id查找进程id

php - Android MySQL-PHP 数据库字符串不通过问题