Jerry
CDCloudflare Developers
•Created by Jerry on 12/28/2023 in #pages-help
How can I connect kv in my local env?
I added kv into the context.env and added kv binding on Cloudflare worker, it works on cloudflare worker now.
But the local env is using wrangler and when I configured kv_namespaces in wrangler.toml, --kv in wrangler run command and tried to get value from kv, it threw an error: 'Cannot read properties of undefined (reading 'get')'.
How do I get kv to run properly locally?
The following are all my code about kv
package.json:
"scripts": {
"build": "prisma generate --data-proxy && remix build",
"dev:remix": "remix watch",
"dev:wrangler": "cross-env NODE_ENV=development yarn run wrangler:dev",
"dev": "npm-run-all build --parallel "dev:"",
"start": "cross-env NODE_ENV=production yarn run wrangler:dev",
"wrangler:dev": "wrangler pages dev ./public --kv=local_kv",
"typecheck": "tsc",
"format": "prettier --write ."
},
wrangler.toml:
kv_namespaces = [
{ binding = "local_kv", id = "", preview_id = "*" }
]
remix.env.d.ts:
export type Environment = {
ENVIRONMENT: string;
KV: KVNamespace;
};
export type SpendrEventContext = EventContext<Environment, any, any>;
export type SpendrEventContextWithAuth = {
auth: any;
db: PrismaClient;
} & SpendrEventContext;
export type LoaderArgs = DataFunctionArgs & {
context: SpendrEventContextWithAuth;
};
export type ActionArgs = DataFunctionArgs & {
context: SpendrEventContextWithAuth;
};
routes loader function:
export async function loader({ request, context }: LoaderArgs) {
const value = await context.env.KV.get('test')
return json({ test: value })
}
2 replies