Closed joelket23 closed 5 days ago
It looks like you are missing setupDevPlatform()
in your next.config.mjs
: https://github.com/joelket23/cloudflare-dev/blob/main/next.config.mjs
@irvinebroque I followed the instructions, but I'm still not receiving data from the database.
What's the error?
@irvinebroque I tried this
import { getRequestContext } from '@cloudflare/next-on-pages';
export const config = { runtime: 'edge' };
export default async function Authenticate(req, res) {
const test = { shop: 'staffa' };
try {
const { results } = await process.env.DB.prepare('SELECT * FROM users');
console.log('resut', results);
} catch (error) {
console.log('catch error', error.message);
}
if (test.shop !== 'staff') {
console.log('is not staff');
}
const myVariable = getRequestContext().env.imgz;
try {
const { env, cf, ctx } = getRequestContext();
let kvData = await env.WORDS.get("test");
console.log('kvdata', kvData);
} catch (error) {
console.log("error in kv: ",error.message)
}
return new Response(myVariable, { status: 200 });
}
getting this error ๐
resut undefined
is not staff
error in kv: Cannot read properties of undefined (reading 'get')
@joelket23 could you update your reproduction to incorporate the steps from https://developers.cloudflare.com/pages/framework-guides/nextjs/ssr/get-started/#3-update-nextconfigmjs?
As a secondary note, I see you're running D1 commands with the --remote
flag, which will execute against your production database. Pages uses a local simulator for D1โto query the local database make sure you're passing --local
to your D1 commands.
@penalosa Iโve updated my reproduction repo; you can check it now. I also want to clarify your secondary point about the D1 commands with the --remote flag. I only used that to confirm if my query is correct for fetching data from the database.
And also I want to confirm that the local KV is not connected to Cloudflare KV like it is in Workers, where adding data reflects on Cloudflare KV storage. However, this doesnโt happen with Pages it creates and stores data locally, as shown in the screenshot below.
local KV is not connected to Cloudflare KV like it is in Workers
It sounds like you want to use your remote data while running your project. This is requires wrangler dev โremote
when running your a Workers project. Unfortunately, Pages projects do not support remote-mode.
Ideally your local development would have no impact on your remote data. You can write a script to seed your local data stores with dummy data or data from remote.
I'm going to close this issue since it isn't a bug.
Which Cloudflare product(s) does this pertain to?
D1, KV
What version(s) of the tool(s) are you using?
wrangler@3.72.2
What version of Node are you using?
v22.5.1ย
What operating system and version are you using?
Mac Sonoma 14.6.1 , Mac M1
Describe the Bug
Observed behavior
Both KV and D1 databases are not working locally; they function fine with Workers but fail to work in Pages. As you can see from my log, KV and D1 are successfully bound, yet I'm still receiving null data. However, when I access D1 directly from the terminal using the command
npx wrangler d1 execute prod-yeniseysk --remote --command="SELECT * FROM users"
, it works correctlyExpected behavior
Both KV and D1 databases must work fine locally, as they do with Workers
Steps to reproduce
Please provide the following:
wrangler.toml
file and add the following code:[[kv_namespaces]] binding = "WORDS" id = ""
preview_id = ""
[[d1_databases]] binding = "DB" database_name = "prod-yeniseysk" database_id = ""
preview_database_id = ""
[dev] ip = "127.0.0.1" port = 8181 inspector_port = 8989 local_protocol = "http" upstream_protocol = "http"
export const config = { runtime: 'edge' }; export default async function Authenticate(req, res) { const test = { shop: 'staffa' };
try { const { results } = await process.env.DB.prepare('SELECT * FROM users'); console.log("resut",results)
} catch (error) { console.log("catch error",error.message) } if (test.shop !== 'staff') { console.log('is not staff'); } let kvData = await process.env.WORDS.get("joyboy") console.log("kvdata",kvData) return new Response('test', { status: 200 }); }