【发布时间】:2018-08-15 03:14:17
【问题描述】:
代码:
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);
});
我尝试了什么:
我尝试使用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' }.................
我想要什么:
在 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).
我的问题:
例如,每当我尝试console.log(rowss[0].username); 时,我都会得到undefined。这适用于所有对象。所以userid,lvl等也是。
【问题讨论】:
标签: mysql node.js node-mysql