-
-
Notifications
You must be signed in to change notification settings - Fork 448
Open
Labels
bugSomething isn't workingSomething isn't working
Description
What version of Elysia is running?
1.4.21
What platform is your computer?
Microsoft Windows NT 10.0.26100.0 x64
What environment are you using
Bun: 1.3.0
Are you using dynamic mode?
No
What steps can reproduce the bug?
Use this script
import { Elysia } from "elysia";
import { SQL } from "bun";
// Connect to a local postgres instance
// Make sure to set these env vars or change the values to match your DB
const sql = new SQL({
url: process.env.DATABASE_URL || "postgres://postgres:postgres@localhost:5432/postgres",
});
// Setup a simple table and data for demonstration
try {
await sql`CREATE TABLE IF NOT EXISTS elysia_repro_users (id SERIAL PRIMARY KEY, name TEXT)`;
// Insert some data if empty
const count = await sql`SELECT COUNT(*) as count FROM elysia_repro_users`;
if (count[0].count === 0n) { // Bun SQL returns BigInt for count
await sql`INSERT INTO elysia_repro_users (name) VALUES ('Alice'), ('Bob')`;
}
} catch (e) {
console.error("Failed to setup DB (ignore if just checking reproduction code structure):", e);
}
const app = new Elysia()
.get("/", async () => {
// Fetch data using Bun's native SQL
// This returns an array of objects (Postgres rows)
const users = await sql`SELECT * FROM elysia_repro_users`;
console.log("Users from DB:", users);
console.log("Type of users:", typeof users);
console.log("Is array?", Array.isArray(users));
// The Issue:
// Elysia should serialize this as JSON.
// However, if the response is coming as [object Object][object Object],
// it implies Elysia is using .toString() instead of JSON.stringify()
// or the Content-Type is inferred incorrectly.
return users;
})
.listen(3000);
console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`);
console.log(`Querying database: ${process.env.DATABASE_URL || "postgres://postgres:postgres@localhost:5432/postgres"}`);What is the expected behavior?
Response should be valid JSON:
[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]What do you see instead?
Response is a string concatenation of object representations:
[object Object][object Object]Additional information
Workaround
Manually serializing the response or using a mapResponse hook fixes the issue.
Example workaround in Elysia app:
new Elysia()
.mapResponse(({ responseValue, set }) => {
if (typeof responseValue === "object" && responseValue !== null) {
// Force JSON serialization
const text = JSON.stringify(responseValue);
set.headers["Content-Type"] = "application/json; charset=utf-8";
return new Response(text);
}
})Have you try removing the node_modules and bun.lockb and try again yet?
No
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working