Cannot access kv namespace

Im trying to make a cache system using kv namespace in wrangler and im trying to acesss it like so env.name.get('key') but it gives reference error env not defined. also i have added the namespace name, id and preview_id to wrangler.toml file help pls
6 Replies
wen
wenOP3mo ago
gimme a couple hours i'll get home and send it app.js:
app.get('/geo/:id', async (c) => {
let data = await env.visitor.get(`${c.req.param('id')}`);
console.log(data);
if (!data) {
let req = await fetch(`https://api.gapi.io/?key=[...]&i=${c.req.param('id')}`);
data = await req.json();
await env.visitor.put(`${c.req.param('id')}`, JSON.stringify(data), { expirationTtl: 172800 });
} else {
data = JSON.parse(data);
}
return c.json(data);
});
app.get('/geo/:id', async (c) => {
let data = await env.visitor.get(`${c.req.param('id')}`);
console.log(data);
if (!data) {
let req = await fetch(`https://api.gapi.io/?key=[...]&i=${c.req.param('id')}`);
data = await req.json();
await env.visitor.put(`${c.req.param('id')}`, JSON.stringify(data), { expirationTtl: 172800 });
} else {
data = JSON.parse(data);
}
return c.json(data);
});
kv_namespaces = [{binding = "visitor", id = "...", preview_id = "..."}]
kv_namespaces = [{binding = "visitor", id = "...", preview_id = "..."}]
wrangler.toml: @Leo ah... i was following a diff page on google i'll read the docs n lyk if i got problems do i not need the line in wrangler.toml ?? i got all that by running some wrangler cmds also this line
const app = new Hono<{ Bindings: Bindings }>()
const app = new Hono<{ Bindings: Bindings }>()
does this mean that evertime i restart my api the data will be erased ??
gwapes
gwapes3mo ago
This is for the type or interface for your bindings, type Bindings = { kv: KVNamespace } Your env can be accessed by using c.env
wen
wenOP3mo ago
im on js not ts... so what what would this be fr js @gwapes
gwapes
gwapes3mo ago
You can only use generics in ts, so ignore it ^
wen
wenOP3mo ago
ok
const hono = require('hono').Hono;
const app = new hono();
const { getConnInfo } = require('hono/cloudflare-workers');
const { cors } = require('hono/cors');

app.use('/*', cors());

app.get('/', (c) => c.text('GET /'));
app.get('/geo/:id', async (c) => {
let data = await c.env.visitor.get(`${c.req.param('id')}`);
console.log(data);
if (!data) {
let req = await fetch(`https://api.gapi.io/?key=[...]&i=${c.req.param('id')}`);
data = await req.json();
await c.env.visitor.put(`${c.req.param('id')}`, JSON.stringify(data), { expirationTtl: 172800 });
} else {
data = JSON.parse(data);
}
return c.json(data);
});
const hono = require('hono').Hono;
const app = new hono();
const { getConnInfo } = require('hono/cloudflare-workers');
const { cors } = require('hono/cors');

app.use('/*', cors());

app.get('/', (c) => c.text('GET /'));
app.get('/geo/:id', async (c) => {
let data = await c.env.visitor.get(`${c.req.param('id')}`);
console.log(data);
if (!data) {
let req = await fetch(`https://api.gapi.io/?key=[...]&i=${c.req.param('id')}`);
data = await req.json();
await c.env.visitor.put(`${c.req.param('id')}`, JSON.stringify(data), { expirationTtl: 172800 });
} else {
data = JSON.parse(data);
}
return c.json(data);
});
so i should be able to use this? @gwapes
gwapes
gwapes3mo ago
Try it and see
Want results from more Discord servers?
Add your server