How do I define the KV_NAMESPACE?

I cannot get my KV_NAMESPACE to work in the worker. I'm following the. tutorial but keep getting errors such as ```TypeError: Cannot read properties of undefined (reading 'put')
11 Replies
Sia
SiaOP•12mo ago
One thing that is poor with the docs is that there isn't actually a working repo, and the docs keep switching between wrangler and other configs. It is really messy to try and understand.
kian
kian•12mo ago
Get started · Cloudflare Workers KV
Workers KV provides low-latency, high-throughput global storage to your Cloudflare Workers applications. Workers KV is ideal for storing user …
kian
kian•12mo ago
This takes you through creating the namespace, binding it and using it in a Worker.
Nikita Savchenko
Nikita Savchenko•12mo ago
For local devt/testing it's not that obvious, I had to figure it out by myself: 1. Use wrangler.toml as on the screenshot, kv namespace's id doesn't matter 2. In package.json, add script "pretest": "wrangler kv:bulk put ./src/Tests/stubs/kv/endpoints.json --local --binding=endpoints" 3. In that stubs file, define kv pairs [{"key":"your-key","value":"your-value"}] 4. When using typescript, add your kv namespace name to worker-configuration.d.ts, like interface Env { yourKvBindingName: KVNamespace; } Any feedback is welcome btw. I'd like to have a comprehensive testing suite for my worker.
No description
Sia
SiaOP•12mo ago
I've figured it out... boy is it poorly written. Any admins around here? I'm happy to improve the docs, if that's okay? 🙂 I did all of that, but it isn't that good 😦
kian
kian•12mo ago
You can open an issue on https://github.com/cloudflare/cloudflare-docs, or make a Pull Request - it's up to you.
Sia
SiaOP•12mo ago
For example, the binding requires capital letters, even if the binding is written in lower case letters when you get it back... omg, now I see why...my fault, or my editors fault.
Sia
SiaOP•12mo ago
No description
Sia
SiaOP•12mo ago
When file isn't saved, the old letter combo is uses. Hence the miss match. That said, the docs do switch between the two, making it hard to follow, and also giving the impression that capitals don't matter. @kian if I got a pages worker, in the functions folder, do I create a toml there to make setup the KV-binding? @kian I read it says I should be able to set it in the dashboard, I went to the correct place but unsure how to define the key and the value in there. I tried setting the key to the value I use in my JS, and the value to the KV-Namespace ID, but it didn't seem to work. @Community Champion Could any of you look at this?
Unsmart
Unsmart•12mo ago
?pings
Flare
Flare•12mo ago
Please do not ping community members for non-moderation reasons. Doing so will not solve your issue faster and will make people less likely to want to help you.
Want results from more Discord servers?
Add your server