SvelteKit D1 example works in local but not after deployed to Cloudflare pages

I can get data from D1 in local:
npx wrangler pages dev .svelte-kit/cloudflare

but when I deploy to Cloudflare Pages via Gitlab CI, I just got an empty exception without error.
npx wrangler pages deploy .svelte-kit/cloudflare --project-name sveltekit-d1 --branch main


This is my code src/routes/api/customers/+server.ts:
import type { RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async ({request, platform}) => {
    let log: string[] = []
    try {
        const d1Db = await platform.env.D1_DB
        log.push("d1 db ok")

        const stmt = await d1Db.prepare(
            "SELECT * FROM Customers LIMIT 5"
            )
        log.push("stmt ok")

        const { results } = await stmt.all();
        log.push("results ok")

        return new Response(JSON.stringify({
            status: 200,
            data: results, 
            log: log
        }));
    } catch (e) {
        return new Response(JSON.stringify({
            status: 500,
            log: log,
            error: e
        }));
    }
};


response from local:
{"status":200,"data":[{"CustomerId":1,"CompanyName":"Alfreds Futterkiste","ContactName":"Maria Anders"},{"CustomerId":4,"CompanyName":"Around the Horn","ContactName":"Thomas Hardy"},{"CustomerId":11,"CompanyName":"Bs Beverages","ContactName":"Victoria Ashworth"},{"CustomerId":13,"CompanyName":"Bs Beverages","ContactName":"Random Name"}],"log":["d1 db ok","stmt ok","results ok"]}


response when deployed on Cloudflare Pages:
{"status":500,"log":["d1 db ok"],"error":{}}


And I already added the D1 database binding variables on the Cloudflare Pages Settings

Not sure what did I miss, thanks.
Was this page helpful?