Son
Son3y ago

Object keeps getting overwritten

I feel like i'm missing something fundamental here. when I add a new entry into object b it resets to an empty object upon a subsequent request. Can anyone help?
addNewTicket: ticketsDomainProcedure
.input(
z.object({
id: z.string(),
title: z.string(),
description: z.string(),
type: z.string(),
status: z.string(),
})
)
.mutation(async (req) => {
const b = {};

console.log('original', b);

const tenantId = '908e2d94-aef7-4ba6-a1a3-2ccec65a1d97';

const updatedState = {
...b,
[req.input.id]: {
id: req.input.id,
tenantId: tenantId,
title: req.input.title,
description: req.input.description,
type: req.input.type,
status: req.input.status,
},
};

console.log('NEW STATE', updatedState);

return updatedState;
}),
});
addNewTicket: ticketsDomainProcedure
.input(
z.object({
id: z.string(),
title: z.string(),
description: z.string(),
type: z.string(),
status: z.string(),
})
)
.mutation(async (req) => {
const b = {};

console.log('original', b);

const tenantId = '908e2d94-aef7-4ba6-a1a3-2ccec65a1d97';

const updatedState = {
...b,
[req.input.id]: {
id: req.input.id,
tenantId: tenantId,
title: req.input.title,
description: req.input.description,
type: req.input.type,
status: req.input.status,
},
};

console.log('NEW STATE', updatedState);

return updatedState;
}),
});
2 Replies
Dani;
Dani;3y ago
tRPC procedures re-run on every request, so every time a request comes in, it creates a new object b. To preserve state you'll have to declare the object outside of the trpc routers/procedures, or better yet, use some sort of database
Son
SonOP3y ago
thank you!

Did you find this page helpful?