converting bigint

When getting the session, I am encountering a problem that the json that contains the user and the session cannot convert because both are bigint. Is there a solution for this?
# SERVER_ERROR: 187 | headers.set("Content-Type", "application/x-www-form-urlencoded");
188 | } else if (data instanceof ReadableStream) {
189 | body = data;
190 | headers.set("Content-Type", "application/octet-stream");
191 | } else if (isJSONSerializable(data)) {
192 | body = JSON.stringify(data);
^
TypeError: JSON.stringify cannot serialize BigInt.
at toResponse (/Users/artur/Developer/project/node_modules/better-call/dist/index.js:192:17)
# SERVER_ERROR: 187 | headers.set("Content-Type", "application/x-www-form-urlencoded");
188 | } else if (data instanceof ReadableStream) {
189 | body = data;
190 | headers.set("Content-Type", "application/octet-stream");
191 | } else if (isJSONSerializable(data)) {
192 | body = JSON.stringify(data);
^
TypeError: JSON.stringify cannot serialize BigInt.
at toResponse (/Users/artur/Developer/project/node_modules/better-call/dist/index.js:192:17)
Solution:
I just added this to the main backend file and it works ```ts declare global { interface BigInt { toJSON(): string...
Jump to solution
6 Replies
arwat
arwatOP2w ago
In the database, I made the ID a bigint and in databaseHooks I replace the string generated by the library with a bigint
databaseHooks: {
user: {
create: {
before: async (user) => {
const snowflakeId = snowflake.nextId()
user.id = snowflakeId.toString()

return {
data: {
...user,
},
}
},
},
},
}
databaseHooks: {
user: {
create: {
before: async (user) => {
const snowflakeId = snowflake.nextId()
user.id = snowflakeId.toString()

return {
data: {
...user,
},
}
},
},
},
}
Ping
Ping2w ago
There isn't a official support for custom IDs just yet. So for now we'll just have to see if we can find some workarounds for this to work. What Database adapter are you using?
arwat
arwatOP2w ago
drizzle
Solution
arwat
arwat2w ago
I just added this to the main backend file and it works
declare global {
interface BigInt {
toJSON(): string
}
}

BigInt.prototype.toJSON = function () {
return this.toString()
}
declare global {
interface BigInt {
toJSON(): string
}
}

BigInt.prototype.toJSON = function () {
return this.toString()
}
is it enough or do it differently?
Ping
Ping2w ago
Hmm yeah I guess so.
arwat
arwatOP2w ago
noice, I'm closing and waiting for an update

Did you find this page helpful?