node mysql2 can't access update results

Viewed 2852

I'm doing a simple UPDATE with mysql2 :

   UPDATE table1
    SET table1.value1 = ?, table1.value2 = ? 
    WHERE user_id = (
      SELECT user_id 
      FROM user
      WHERE company_id = ?
    ) 

table1 is related to the user table by user_id, but I only have the company_id so I do a subquery (probably should be a join, but thats another discussion):

const [results, buff] = await connection.execute(query, values);
return results

However, when accessing the results to make sure there are no errors and that only a single row is updated, results returns:

  console.log
    ResultSetHeader {
      fieldCount: 0,
      affectedRows: 1,
      insertId: 0,
      info: 'Rows matched: 1  Changed: 0  Warnings: 0',
      serverStatus: 2,
      warningStatus: 0,
      changedRows: 0
    }

But I can't access the values. If I try results.affectedRows I get

Property 'affectedRows' does not exist on type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[]'.

The only way to make it work is to do results['affectedRows']. Any suggestions?

1 Answers

So I forgot to mention that this is a typescript error, and I was able to fix it by doing:

const [results, buff] = await connection.execute(query, values);
const json: any = results;
return json.affectedRows //or whatever property I want

This also works:

const [results, buff] = await connection.execute(query, values);
return json as any; //then in the calling fuction access the properties I want
Related