Read Data from Cloudflare Workers KV with BINDING.get

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 weeks ago

To access that data that we stored in our database, we use the .get() function. .get() is also an async function and we will need to await it.

Instructor: [0:00] Now that we've put something into our KV namespace, for instance, this value string here for this key, test-key, let's understand how to look something back up from that KV namespace.

[0:11] In order to do that, I'll use MyFirstKv.get. GET is the complementary function to PUT, which takes in one argument, a key. In this case, that will be test-key. What I'll get back here is a promise that resolves to be value of this KV key. I'll say const value=await MyFirstKv.get("test-key").

[0:38] Finally, I'll replace "Hello, worker" here with value, so I can see my KV value coming back from the KV namespace. To test this, I'll run wrangler publish. If I open this in browser, you can see that I get value back here. This is, of course, this value here, which was put as the value for test-key inside of MyFirstKv namespace.

[1:01] What we have here is first, we're putting something into KV and then second, we're reading it back out of that same key in order to return it as the value inside of this response.