T
tRPC

Object keeps getting overwritten

Object keeps getting overwritten

TTCI11/11/2022
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;
}),
});
DDani;11/11/2022
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
TTCI11/14/2022
thank you!

Looking for more? Join the community!

T
tRPC

Object keeps getting overwritten

Join Server