object updating incorrectly

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;
}),
});
1 Reply
Froxx
Froxx3y ago
I don't really understand what you're trying to do. You're never writing anything to b. You're just defining it doing const b = {}; and spread it afterwards doing const updatedState = { ...b, andsoon, ... } b never contains any props that could be spread
Want results from more Discord servers?
Add your server