H
Hono4mo ago
iEmix

Context how it work?

export type AuthUser = { id?: number name?: string; email?: string; }; i want have c.get('user'); and c.set('user', authUser); but i recive Argument type "user" is not assignable to parameter type keyof E["Variables"] and Argument type "user" is not assignable to parameter type keyof E["Variables"] #context
1 Reply
Aditya Mathur
Aditya Mathur4mo ago
Basically Hono is typesafe, so if you are planning to add some variables in the context you will need to add them to the Hono Variables, which is beneficial as you will get intelli sense and all the typings. Here is how you can do it
const app = new Hono<{ Variables: { user: Record<string, unknown> } }>();

app.use(async (c, next) => {
c.set("user", { "name" : "meow" })

await next();
})

app.get((c) => {
return c.json(c.get("user"))
})
const app = new Hono<{ Variables: { user: Record<string, unknown> } }>();

app.use(async (c, next) => {
c.set("user", { "name" : "meow" })

await next();
})

app.get((c) => {
return c.json(c.get("user"))
})
Want results from more Discord servers?
Add your server