0

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" 
5
  • Instead of using a ...data rest pattern, better be explicit about what properties exactly you want in the final object, like send({ updatedAt: data.updatedAt, country: data.shared.country, … }). Commented Dec 14, 2019 at 16:27
  • You mean .send({data: {...data, updatedAt, createdAt }}) ? Commented Dec 14, 2019 at 16:28
  • 3
    About the $init: true I guess account is 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 using toObject. Commented Dec 14, 2019 at 16:30
  • const acc = account.toObject(); const { email, loggedIn, location, ...data } = acc.shared; const { updatedAt, createdAt } = acc; Commented Dec 14, 2019 at 16:42
  • 1
    @Bill I would guess it should look like that. It has been a long time when I last used mongodb. Commented Dec 14, 2019 at 16:47

3 Answers 3

1

Use spread operator:

{ ...data, updatedAt, createdAt } 

Be warning, your variable name gonna be your index

Sign up to request clarification or add additional context in comments.

Comments

1

To remove $init but nothing else.

const {$init, ...cleanData} = data; 

To create a data object with createdAt and updatedAt

const finalData = {...cleanData, createdAt, updatedAt} 

Happy coding!

2 Comments

It is true that you can get rid of the $init in that way. But if it shouldn’t be there in the first place then the correct way in getting rid of it is to figure out where/why it was added and ensure that it is not added.
There were already comments addressing how to convert Mongoose documents into POJOs. I thought it would be better to add useful information for others dabbling with rest/spread. Over and out.
0

Just use .toObject() like so:

static profile = async (req: Request, res: Response) => { try { const { username } = req.params; const _account = await userModel .findOne({ 'shared.username': username }) .exec(); const account = _account.toObject() if (account) { const {email, loggedIn, location, warningMessage, ...data} = account.shared; const { updatedAt, createdAt } = account; res .status(200) .send({ data: { ...data, ...{ updatedAt, createdAt }} }); } else res.status(400).send({ message: 'Server Error' }); } catch (error) { res.status(400).send({ message: 'Server Error', error }); } 

};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.