Hax
Hax10mo ago

Context being destroyed in mutator

Im currently building an app using the t3 stack + clerk for auth I have an operation which relies on a user's id being passed in my trpc backend, which on other routers was being passed fine through the context object Specifically within this router and this mutator method, I am getting the values from context up until my prisma method is called (I think) and then they disappear. The mutator method is getting the context value, as I console.logged it before hitting this specfic api route Things attempted: - Merge existing ctx object with new ctx object - run middleware as async with awaits, - Any sort of insight or other things I can try is appreciated Code snippets in comments
Solution:
Solution: pass uid in from frontend using custom hook
Jump to solution
2 Replies
Hax
Hax10mo ago
Code snippets: Error Msg (Undefined property):
tRPC failed on workoutLog.createLog: [
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": [
"sets",
0,
"userId"
],
"message": "Required"
},
tRPC failed on workoutLog.createLog: [
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": [
"sets",
0,
"userId"
],
"message": "Required"
},
Mutator Method:
createLog: protectedProcedure
.input(workoutSchema)
.mutation(async({ctx, input}) => {
const {notes, workoutName, trainingPlanId, sets} = input;
const {userId} = ctx.auth
const userIdSets = sets.map(set => {return {...set, userId}})
try {
// context exists until here... where it's getting destroyed
const dbLog = await prisma.workoutLog.create({
data: {
trainingPlanId,
notes,
name: workoutName,
authorId: userId,
sets: {
createMany: {
data: userIdSets
}
}
}
})
return dbLog;
} catch (err) {
return {err: err, ctx: ctx}
}
}),
createLog: protectedProcedure
.input(workoutSchema)
.mutation(async({ctx, input}) => {
const {notes, workoutName, trainingPlanId, sets} = input;
const {userId} = ctx.auth
const userIdSets = sets.map(set => {return {...set, userId}})
try {
// context exists until here... where it's getting destroyed
const dbLog = await prisma.workoutLog.create({
data: {
trainingPlanId,
notes,
name: workoutName,
authorId: userId,
sets: {
createMany: {
data: userIdSets
}
}
}
})
return dbLog;
} catch (err) {
return {err: err, ctx: ctx}
}
}),
Middleware/Context (trpc.ts)
const enforceUserIsAuthed = t.middleware(async({ ctx, next }) => {
if (!ctx.auth.userId) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return await next({
ctx: {
auth: ctx.auth
},
});
});
const enforceUserIsAuthed = t.middleware(async({ ctx, next }) => {
if (!ctx.auth.userId) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return await next({
ctx: {
auth: ctx.auth
},
});
});
interface AuthContext {
auth: SignedInAuthObject | SignedOutAuthObject
}

const createInnerTRPCContext = ({auth} : AuthContext) => {
return {
prisma,
auth,
}
}

/**
* This is the actual context you will use in your router. It will be used to process every request
* that goes through your tRPC endpoint.
*
* @see https://trpc.io/docs/context
*/
export const createTRPCContext = (opts: CreateNextContextOptions) => {
const { req, res } = opts;
return createInnerTRPCContext({auth: getAuth(req)});
};
interface AuthContext {
auth: SignedInAuthObject | SignedOutAuthObject
}

const createInnerTRPCContext = ({auth} : AuthContext) => {
return {
prisma,
auth,
}
}

/**
* This is the actual context you will use in your router. It will be used to process every request
* that goes through your tRPC endpoint.
*
* @see https://trpc.io/docs/context
*/
export const createTRPCContext = (opts: CreateNextContextOptions) => {
const { req, res } = opts;
return createInnerTRPCContext({auth: getAuth(req)});
};
Solution
Hax
Hax10mo ago
Solution: pass uid in from frontend using custom hook