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
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
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
}));
}
};
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"]}
{"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":{}}
{"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.
No description
3 Replies
ryanlee7537
ryanlee753710mo ago
vnphanquang
vnphanquang10mo ago
Thanks @ryanlee7537 , scratching my head off today for this 🙏
divby0
divby09mo ago
@ryanlee7537 Does local dev with bindings work for you without the 4-5 seconds delay of completely rebuilding and using wrangler pages dev? For me this takes 5 seconds and I then have to manually refresh the page (loosing so much of sveltekit's advantages)