Timo Martinson
Timo Martinsonā€¢2y ago

KV: What to return from here?

// Create City
router.post('/cities', async (context) => {
const data = await context.request.body().value
if (data.slug && data.name) {
await kv.set(['cities', data.slug], {
slug: data.slug,
name: data.name,
description: data.description
} satisfies ICity)
}
})
// Create City
router.post('/cities', async (context) => {
const data = await context.request.body().value
if (data.slug && data.name) {
await kv.set(['cities', data.slug], {
slug: data.slug,
name: data.name,
description: data.description
} satisfies ICity)
}
})
7 Replies
Timo Martinson
Timo MartinsonOPā€¢2y ago
let me add this: request: { url: "http://localhost:4040/cities", method: "POST", hasBody: true } response: { status: 404, type: undefined, hasBody: false, writable: true } This is the error. Internal Server Error. šŸ˜¦
ioB
ioBā€¢2y ago
what framework are you using also, why not use
await kv.set<ICity>(['cities', data.slug], {
slug: data.slug,
name: data.name,
description: data.description
})
await kv.set<ICity>(['cities', data.slug], {
slug: data.slug,
name: data.name,
description: data.description
})
instead of trying to hack it in with "satisfies"
Timo Martinson
Timo MartinsonOPā€¢2y ago
ok with OAK what is this: Expected 0 type arguments, but got 1.deno-ts(2558) interface ICity
ioB
ioBā€¢2y ago
ah sorry, looks like that must have misremembered this functionality, disregard my prior comment what error are you getting?
Timo Martinson
Timo MartinsonOPā€¢2y ago
Internal Server Error šŸ˜¦ and when i make a get-request: 404 not found
// List Cities
router.get('/cities', async () => {
const list = kv.list<ICity[]>({ prefix: ['cities'] })
const cities = []
for await (const city of list) cities.push(city)
return { data: { cities } }
})
// List Cities
router.get('/cities', async () => {
const list = kv.list<ICity[]>({ prefix: ['cities'] })
const cities = []
for await (const city of list) cities.push(city)
return { data: { cities } }
})
ioB
ioBā€¢2y ago
looking at the docs, i have no idea where you're getting a return statement from don't you have to set ctx.response.body?
Timo Martinson
Timo MartinsonOPā€¢2y ago
oh yes thank you i forgot šŸ˜¦ silly me ssooooooo... GET works, POST doesn't ok now it works

Did you find this page helpful?