Monday, 29 May 2017

mysql node returning unwanted results

node beginner here, but experienced with javascript.

I'm executing this query in node mysql, along with the promise-mysql package. The objective is to insert a new user if not exists, and get the user data afterwards.

SET @email = ?;
INSERT IGNORE INTO users 
(id,email, first_name, last_name,fb_user_id) 
VALUES (0,@email,?,?,?);
SELECT * FROM users WHERE email = @email;

Example usage, where sql is the query above.

return db.query(sql,[email,first_name,last_name,fbUserId])
    .then((results)=>{
        console.log(results)
        return results;
    })

The results object looks like this

[ 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 } ] ]

What are these OkPackets? Why can't i get a simple array of the data, instead of this meta data. Do i have to do var user = results[2][0]??. Please tell me what i am missing, because these arguments seem wrong. There's no way i should have to manually parse this argument for every query i'll eventually write..



via Eric Guan

No comments:

Post a Comment