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
znix
znix3y ago
Hello, im not an expert but whats export const mergeRouter = t.mergeRouters;?
pradeep800
pradeep800OP3y 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
znix
znix3y ago
can u go to ur network tab and check ur payload when u make the request
pradeep800
pradeep800OP3y ago
i don't why but there are lot of undefine
znix
znix3y ago
seems like ur form is not sending correct data
pradeep800
pradeep800OP3y 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?
znix
znix3y 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
pradeep800
pradeep800OP3y ago
will it make any differnce?
znix
znix3y ago
for the current error no... but best practice
pradeep800
pradeep800OP3y 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
znix
znix3y ago
i think best practice would be to make an authRouter and put both functions inside it
pradeep800
pradeep800OP3y 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
znix
znix3y 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.
pradeep800
pradeep800OP3y ago
now
pradeep800
pradeep800OP3y ago
working
pradeep800
pradeep800OP3y ago
there is something wrong in after 95 line are you able to figure out what is wrong with these last line of code ?
znix
znix3y ago
can u uncomment all and comment the merge line
pradeep800
pradeep800OP3y ago
what do you mean by that merge line
znix
znix3y ago
like comment line 130 export const mergeRouter = t.mergeRouters;
pradeep800
pradeep800OP3y ago
i already did but it is influencing any another line
znix
znix3y ago
ig u can try uncommenting the middleware line by line see where it breaks
pradeep800
pradeep800OP3y 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
znix
znix3y 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");
pradeep800
pradeep800OP3y ago
pradeep800
pradeep800OP3y 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? ?
znix
znix3y 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
pradeep800
pradeep800OP3y ago
not working what public variable btw
znix
znix3y ago
im not sure if this is the way tho its not running in browser im lost wait for someone else to help
pradeep800
pradeep800OP3y ago
But jwt key in public env can be excessed by anyone and then there is no point of authentication
znix
znix3y ago
yeah thats why im confused + its not in browser anyway
pradeep800
pradeep800OP3y ago
Ok If anyone sees this can you tell me why env is not working in the trpc file???
znix
znix3y ago
just tested it, it works fine for me?
znix
znix3y ago
znix
znix3y ago
i can even pull db string from env works just fine
znix
znix3y ago
even using process.
pradeep800
pradeep800OP3y ago
yeah same here const data = jwtPayload.parse(userInfo); console.log(data); this statemnt is not not working
pradeep800
pradeep800OP3y ago
znix
znix3y ago
whats .parse? whats userinfo?
pradeep800
pradeep800OP3y 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
znix
znix3y ago
use safeparse func instead
pradeep800
pradeep800OP3y ago
yeah it start working now thank for you help
znix
znix3y ago
👍
pradeep800
pradeep800OP3y ago
how can i close this qeustion?
znix
znix3y ago
right click on the left just do close post dont worry about it too much

Did you find this page helpful?