Home > Enterprise >  MariaDB response from Insert Statement
MariaDB response from Insert Statement

Time:09-28

I am currently building a small application and everything worked so far, but now I am really confused about a return value.

I am using a MariaDB Database with NodeJS and the mariadb connector. All the statements pass without failure, but when I send an INSERT Statement, the DB sends back this:

OkPacket {affectedRows: 1, insertId: 51n, warningStatus: 0}
affectedRows:
1
insertId:
51n
warningStatus:
0

The Database is fine with the statement, but I don't get where the "n" behind the inserted ID is coming. The problem is, it breaks the complete application, because JSON.stringify() says:

TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {stack: 'TypeError: Do not  know how to serialize a Big…ions (node:internal/process/task_queues:96:5)', message: 'Do not know how to serialize a BigInt'}

So it seems, that the DB sends back the ID, which is the primary key of this table, as a BigInteger and JSON.stringify() can not parse this. But I don't want a BigInteger in the response.

Can anyone help me with this? Thx in advance

CodePudding user response:

You can pass options to the query method of the connection to return the insertId as string.

await conn.query(stmt, { supportBigNumbers: true, bigNumberStrings: true})

Alternately, you can return it as a Number. The method throws an exception if the insertId can't be converted from a BigInt to a Number

await conn.query(stmt, { supportBigNumbers: true, insertIdAsNumber: true})
  • Related