节点初学者在这里,但有javascript经验。
我正在节点mysql中执行此查询以及promise-mysql
包。目标是在不存在的情况下插入新用户,然后获取用户数据。
SET @email = ?;
INSERT IGNORE INTO users
(id,email, first_name, last_name,fb_user_id)
VALUES (0,@email,?,?,?);
SELECT * FROM users WHERE email = @email;
示例用法,其中sql
是上面的查询。
return db.query(sql,[email,first_name,last_name,fbUserId])
.then((results)=>{
console.log(results)
return results;
})
results
对象看起来像这样
[ OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 10,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 },
OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 10,
warningCount: 1,
message: '',
protocol41: true,
changedRows: 0 },
[ RowDataPacket {
id: 98,
email: 'surfinchicken@gmail.com',
first_name: 'Bob',
last_name: 'Law',
fb_user_id: 10203453461607024 } ] ]
JSON版本:
[{"fieldCount":0,"affectedRows":0,"insertId":0,"serverStatus":10,"warningCount":0,"message":"","protocol41":true,"changedRows":0},{"fieldCount":0,"affectedRows":0,"insertId":0,"serverStatus":10,"warningCount":1,"message":"","protocol41":true,"changedRows":0},[{"id":98,"email":"surfinchicken@gmail.com","first_name":"Bob","last_name":"Law","fb_user_id":10203453461607024 }]]
这些OkPacket
是什么?为什么我不能得到一个简单的数据数组,而不是这个元数据。我必须做var user = results[2][0]
??。请告诉我我错过了什么,因为这些论点似乎错了。对于我最终写的每个查询,我都不可能手动解析这个参数..