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].userid
和rowss.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/