Cannot access 'createTRPCRouter' before initialization

can anyone please help why this erorr is coming and how i can resolve it https://github.com/pradeep800/to-do-with-t3 Error ReferenceError: Cannot access 'createTRPCRouter' before initialization at Module.createTRPCRouter (webpack-internal:///(api)/./src/server/api/trpc.ts:6:65) at eval (webpack-internal:///(api)/./src/server/api/routers/sigin.ts:23:64) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
GitHub
GitHub - pradeep800/to-do-with-t3
Contribute to pradeep800/to-do-with-t3 development by creating an account on GitHub.
45 Replies
z
z2y ago
Hello, im not an expert but whats export const mergeRouter = t.mergeRouters;?
pradeep
pradeepOP2y ago
before i was creating router i was using merge router which i am not using even if i remove it it will be fine
z
z2y ago
can u go to ur network tab and check ur payload when u make the request
pradeep
pradeepOP2y ago
i don't why but there are lot of undefine
z
z2y ago
seems like ur form is not sending correct data
pradeep
pradeepOP2y ago
in github you can see i am using hand written response and there is no trpc or typescript error and t3 stack is type safe right?
z
z2y ago
seeing a lot of bad practices in ur code besides the error why is signIn and login different routers? ur treating routers as endpoints
pradeep
pradeepOP2y ago
will it make any differnce?
z
z2y ago
for the current error no... but best practice
pradeep
pradeepOP2y ago
it was working before but when i start coding middleware i don't what happen can you tell me what should i do for best practice should i put them in one file
z
z2y ago
i think best practice would be to make an authRouter and put both functions inside it
pradeep
pradeepOP2y ago
i will do it thank you can you give me reset of trpc.ts i am trying to finding it i am unable to find it main repo i think there is something wrong in trpc.ts
z
z2y ago
GitHub
to-do-with-t3/trpc.ts at main · pradeep800/to-do-with-t3
Contribute to pradeep800/to-do-with-t3 development by creating an account on GitHub.
pradeep
pradeepOP2y ago
now
pradeep
pradeepOP2y ago
working
pradeep
pradeepOP2y ago
there is something wrong in after 95 line are you able to figure out what is wrong with these last line of code ?
z
z2y ago
can u uncomment all and comment the merge line
pradeep
pradeepOP2y ago
what do you mean by that merge line
z
z2y ago
like comment line 130 export const mergeRouter = t.mergeRouters;
pradeep
pradeepOP2y ago
i already did but it is influencing any another line
z
z2y ago
ig u can try uncommenting the middleware line by line see where it breaks
pradeep
pradeepOP2y ago
const authMiddleware = t.middleware(({ ctx, next }) => { // let JWTtoken = ctx.req.headers.authorization?.replace("bearer ", ""); // console.log(JWTtoken); // let JWT_SECRET = process.env.JWT_SECRET as string; // let userInfo; // console.log("hello there"); // try { // if (JWTtoken) { // userInfo = jwt.verify(JWTtoken, JWT_SECRET); // } else { // throw Error("not present"); // } // } catch (err) { // throw new TRPCError({ // code: "UNAUTHORIZED", // message: "not_authenticated", // }); // } // const data = jwtPayload.parse(userInfo); return next({ ctx: { id: "hello", name: "nope", }, }); }); still code is working on commented line there is some error
z
z2y ago
comment these
// let JWTtoken = ctx.req.headers.authorization?.replace("bearer ", "");
// console.log(JWTtoken);
// let JWT_SECRET = process.env.JWT_SECRET as string;
// let userInfo;
// console.log("hello there");
// let JWTtoken = ctx.req.headers.authorization?.replace("bearer ", "");
// console.log(JWTtoken);
// let JWT_SECRET = process.env.JWT_SECRET as string;
// let userInfo;
// console.log("hello there");
pradeep
pradeepOP2y ago
pradeep
pradeepOP2y ago
const authMiddleware = t.middleware(({ ctx, next }) => { // let JWTtoken = ctx.req.headers.authorization?.replace("bearer ", ""); // console.log(JWTtoken); // let JWT_SECRET = process.env.JWT_SECRET as string; let userInfo; // console.log("hello there"); const JWTtoken = "nothing"; const JWT_SECRET = "noting"; try { if (JWTtoken) { userInfo = jwt.verify(JWTtoken, JWT_SECRET); } else { throw Error("not present"); } } catch (err) { throw new TRPCError({ code: "UNAUTHORIZED", message: "not_authenticated", }); } i think that file cannot excess env files what do you think? ?
z
z2y ago
try doing it through env file u might not be able to do at all then u have to define it as public variable but thats not recommended
pradeep
pradeepOP2y ago
not working what public variable btw
z
z2y ago
im not sure if this is the way tho its not running in browser im lost wait for someone else to help
pradeep
pradeepOP2y ago
But jwt key in public env can be excessed by anyone and then there is no point of authentication
z
z2y ago
yeah thats why im confused + its not in browser anyway
pradeep
pradeepOP2y ago
Ok If anyone sees this can you tell me why env is not working in the trpc file???
z
z2y ago
just tested it, it works fine for me?
z
z2y ago
z
z2y ago
i can even pull db string from env works just fine
z
z2y ago
even using process.
pradeep
pradeepOP2y ago
yeah same here const data = jwtPayload.parse(userInfo); console.log(data); this statemnt is not not working
pradeep
pradeepOP2y ago
z
z2y ago
whats .parse? whats userinfo?
pradeep
pradeepOP2y ago
you can see in image JwtPayload.parser(userInfo); i am using email in jwt but i am asking for name jwt parser that was problem hopefully let me check
z
z2y ago
use safeparse func instead
pradeep
pradeepOP2y ago
yeah it start working now thank for you help
z
z2y ago
👍
pradeep
pradeepOP2y ago
how can i close this qeustion?
z
z2y ago
right click on the left just do close post dont worry about it too much
Want results from more Discord servers?
Add your server