You're quite close!
You're quite close!
Two small changes needed:
-
KV = "TEST"
assigns the string to the KV variable, instead of the KV namespace. In the old service worker syntax, removing this line would've been your solution, as bindings were global variables. In module workers (ones with export default
), they are available on the env
variable which is the second parameter of your fetch
function in line 4, so on line 5 and 6 you should change it to env.KV.put
and env.KV.get
.
- You'll need to actually bind your worker to this KV namespace you created, which in the dashboard you can do by clicking on Workers & Pages, selecting your worker, clicking on the Settings tab, and scrolling down to KV Namespace bindings. There, you'll need to select the KV namespace you created (Edit Variables, Add binding) and give it a name that it will be made available as (KV
in the example).
Saving it should create a new deployment where the namespace you selected is available as env.KV
4 Replies
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
yup, on the code side this looks all good!
the binding is done in the settings tab
where you map
KV
to any namespace you createdUnknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ah, sorry, been stuck in a call
No problem, glad you found it