Types of OkPacket[], FieldPacket[] of mysql2 in node.js

Viewed 10

I'm using mysql2 in node.js. And I define a variable that receives a result of a delete query, runs the query and displays a part of the result like below.

let deleteRes: [OkPacket[], FieldPacket[]] = [[], []];
deleteRes = await mysqlConnection.query<OkPacket[]>(`DELETE FROM tableName WHERE data1 = ? AND data2 = ?`, [data1, data2]);
console.log(deleteRes[0].affectedRows);

However, a compile error occurred at the console.log line above, when I compile it.

Error:(57, 39) TS2339: Property 'affectedRows' does not exist on type 'OkPacket[]'.

But, the code above works properly when I run the code.
Why does the compile error occur?

For testing, I changed the console.log line to below.

console.log(deleteRes[0][0].affectedRows);

So, the compile error did not occur, but an error occurred when I run the code like below.

TypeError: Cannot read properties of undefined (reading 'affectedRows')

Is there a way to resolve both errors at one time?

OS: MacOS v11.6.7.
IDE: IntelliJ IDEA 2022.1.
Typescript: v4.7.4.
Node.js: v16.17.0

0 Answers
Related