H
Hono3mo ago
uc

hono c.set() showing error but what is wrong and also prisma error

i dont understand what i am doing wrong like i understand the error but don't know how to resolve
No description
No description
10 Replies
Josh
Josh3mo ago
What is the error of c.set if you hover over it? As for the prisma error, in your catch on line 66, you may want to log the error to get the prisma stack trace.
ambergristle
ambergristle3mo ago
the verify helper returns type JWTPayload: https://github.com/honojs/hono/blob/c03f24536833d1edd081df5b62624a95234bacfb/src/utils/jwt/types.ts#L76 this has a couple default claim (number) properties defined, but any other string key is typed unknown the TS server is most likely complaining because the userId context variable is typed as a string, and you're trying to set an unknown value to it without type-checking an easy fix would be to do something like
const payload = await verify(/** */);
// `verify` will always return a truthy value, so no need to check
const userId = payload.id;
if (typeof userId !== 'string') {
return c.json(/** */);
}

c.set('userId', userId);
await next();
const payload = await verify(/** */);
// `verify` will always return a truthy value, so no need to check
const userId = payload.id;
if (typeof userId !== 'string') {
return c.json(/** */);
}

c.set('userId', userId);
await next();
the type error in the middle panel on error.message is a similar issue. you don't have a type guard, so error is typed as unknown
} catch (error) {
const message = error instanceof Error
? error.message
: 'Unknown error';

return c.json({ message }, 500);
}
} catch (error) {
const message = error instanceof Error
? error.message
: 'Unknown error';

return c.json({ message }, 500);
}
side note: 404 errors are only for Not Found, whether the endpoint doesn't exist, or if a queried resource (e.g., a user) doesn't exist in the database. generic server-side errors should use a 500 also probably worth moving the PrismaClient init into custom middleware, but that's more about DX you're getting the PrismaClientValidationError because the data you're passing to prisma in /postblog is invalid or missing: https://www.prisma.io/docs/orm/reference/error-reference#prismaclientvalidationerror this may be because you're not properly type-checking that payload.id exists in your auth middleware (see above), or because you're not correctly passing the post title and/or content in the body you should use some kind of validation any time you're parsing request data: https://hono.dev/docs/guides/validation i would recommend using zod with the zod-validator middleware, but hono has middleware for a number of different validation libraries @Josh is absolutely right though. it's important to include the error message/info when asking for help, and you should always log errors, both for the message and the stack trace consider throwing errors to app.onError instead of returning a response directly in order to reduce repetition: https://hono.dev/docs/api/hono#error-handling
uc
ucOP3mo ago
its working and i am going to used zod for validation and can you suggest lib for hashing password for cloudflare worker. the c.set() error got solved but i think it was due user.id being string and i was passing as number, am i CORRECT ?
No description
No description
uc
ucOP3mo ago
and how do i get access to env.DATABASE_URL for that i cannot use process.env
uc
ucOP3mo ago
GitHub
blogApp/backend at main · uc02/blogApp
Contribute to uc02/blogApp development by creating an account on GitHub.
ambergristle
ambergristle3mo ago
for hashing you can use node:crypto, though you may need to do some config for the polyfill: https://developers.cloudflare.com/workers/runtime-apis/nodejs/crypto/ node-rs is probably also an option. not sure about cloudflare compatability, but idk why it wouldn't work i'd recommend using the Argon2id algorithm. here's some more info on that: https://thecopenhagenbook.com/password-authentication#argon2id i think you're wrong about the c.set type error. the error message is pretty clear: "type unknown is not assignable to parameter of type string the changes you've made in your screenshot are partially correct, but you shouldn't be coercing to a string if typeof user.id !== 'string'. if it's not a string, you should throw an error you can use this factory method to get access to hono Context in your custom middleware: https://hono.dev/docs/helpers/factory#createmiddleware
uc
ucOP3mo ago
i'll use argon2id i seem similar to bcryptjs
ambergristle
ambergristle3mo ago
that works, but you can also just do this, as demonstrated in the link i shared
const app = new Hono();
// ...
// only need to do this once
app.onError((error, c) => {
const message = error instanceof Error
? error.message
: 'Unknown error'

return c.json({ message }, 500)
});
const app = new Hono();
// ...
// only need to do this once
app.onError((error, c) => {
const message = error instanceof Error
? error.message
: 'Unknown error'

return c.json({ message }, 500)
});
uc
ucOP3mo ago
was going to do this and was reading the doc thanks for the help i appreciate it
ambergristle
ambergristle3mo ago
no problem!

Did you find this page helpful?