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;?
cornellexcybertownvip
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
cornellexcybertownvip
i don't why but there are lot of undefine
znix
znix3y ago
seems like ur form is not sending correct data
cornellexcybertownvip
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
cornellexcybertownvip
will it make any differnce?
znix
znix3y ago
for the current error no... but best practice
cornellexcybertownvip
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
cornellexcybertownvip
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.
cornellexcybertownvip
now
cornellexcybertownvip
working
cornellexcybertownvip
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
cornellexcybertownvip
what do you mean by that merge line
znix
znix3y ago
like comment line 130 export const mergeRouter = t.mergeRouters;
cornellexcybertownvip
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
cornellexcybertownvip
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");
cornellexcybertownvip
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
cornellexcybertownvip
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
cornellexcybertownvip
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
cornellexcybertownvip
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.
cornellexcybertownvip
yeah same here const data = jwtPayload.parse(userInfo); console.log(data); this statemnt is not not working
znix
znix3y ago
whats .parse? whats userinfo?
cornellexcybertownvip
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
cornellexcybertownvip
yeah it start working now thank for you help
znix
znix3y ago
👍
cornellexcybertownvip
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?