I'm currently returning createdAt and updatedAt outside of the data object, I want to add them to the data object so I can return a single object.
I've also noticed I'm getting $init: true from somewhere, how to get rid of this?
static profile = async (req: Request, res: Response) => { try { const { username } = req.params; const account = await userModel .findOne({ 'shared.username': username }) .exec(); if (account) { const {email, loggedIn, location, warningMessage, ...data} = account.shared; const { updatedAt, createdAt } = account; res .status(200) .send({ data, updatedAt, createdAt }); // <=== How to combine updatedAt & createdAt into data? } else res.status(400).send({ message: 'Server Error' }); } catch (error) { res.status(400).send({ message: 'Server Error', error }); } }; The current result is
createdAt: "2019-12-13T12:15:05.031Z" data: { $init: true // <=== why is this here, where did it come from? avatarId: "338fcdd84627317fa66aa6738346232781fd3c4b.jpg" country: "AS" fullName: "Bill" gender: "male" language: "en" username: "bill" } updatedAt: "2019-12-14T16:07:34.923Z"
...datarest pattern, better be explicit about what properties exactly you want in the final object, likesend({ updatedAt: data.updatedAt, country: data.shared.country, … })..send({data: {...data, updatedAt, createdAt }})?$init: trueI guessaccountis retrieved from mongo db. IIRC you are not supposed to use the result directly that way but you need to convert it to a plain object usingtoObject.const acc = account.toObject(); const { email, loggedIn, location, ...data } = acc.shared; const { updatedAt, createdAt } = acc;