juico
trpc mutation call stuck
I have an issue with a simple mutation procedure:
getPublicUser: publicProcedure
.input(z.object({
walletAddress: z.string()
}))
.mutation(async ({ ctx, input }) => {
console.log('ctx.prisma: ', ctx.prisma)
try {
const user = await ctx.prisma.users.findUnique({
where: {
walletAddress: input.walletAddress
},
select: {
walletAddress: true,
id: true,
}
})
return user
} catch (e: any) {
console.log('E: ', e)
}
})
It shows in my console that it calls the mutation successfully, but it keeps on being stuck. It does not log the first like in the code even.
My trpc.ts
setup:
const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter({ shape, error }) {
return {
...shape,
data: {
...shape.data,
zodError:
error.cause instanceof ZodError ? error.cause.flatten() : null,
},
};
},
});
export const createTRPCContext = async (_opts: CreateNextContextOptions) => {
const { req, res } = _opts;
async function getUserFromHeader() {
console.log('req.headers.authorization: ', req.headers.authorization)
if (req.headers.authorization && req.headers.authorization.split(' ')[1]) {
try {
const token = localStorage.getItem('crypties:auth')
if (!token) return null;
const parsedToken = JSON.parse(token);
if (new Date(parsedToken.validUntil) < new Date()) return null
console.log('token: ', token)
const user = await prisma.users.findFirst({
where: {
walletAddress: parsedToken.userWalletAddress
},
});
return user;
} catch (error) {
return null;
}
}
return null;
}
return { req, res, user: await getUserFromHeader(), prisma };
};
export const publicProcedure = t.procedure;
18 replies
How do I set a context dynamically?
I don't fully understand middleware in trpc and especially using it for adminProcedures.
So my trpc.ts looks like this:
What I don't understand is how do I inject user into the context?
I get user with public procedure:
Now I try to inject it inside the procedure but when I console log the user inside the middleware function it shows undefined.
2 replies
Type return error when using mongoose.
node - v16.15.1
npm
I'm somewhat new to trpc. Using it with mongoose. Love it so far althought I do have a problem with how types are returned.
For example I have a simple procedure:
This should return either UserDocument or null.
I call it in my client:
But this gives error:
So it seems to be returning a different type than specified.
8 replies