How to wait for all the data to be set before saving in mongoose?

Viewed 29

I am trying to set or udpate undefined (or defined) values in mongoose but I am having hard time because I need to await for the values. This code doesn't save the data, I think it is because the USER.save() executes before the values are set? How can I wait for the data to be udpated/set before saving?

const USER = await User.findById(await UserID)

USER.authToken = await authToken,
USER.displayName = await name,
USER.profilePic = await pic
      
await USER.save()
1 Answers

Okay I got it figured, when the authToken was set it triggered the .save() method so I just added two more .save() and it works so I am happy. Sorry for being unclear in my question guys.

USER.authToken = await authToken
await USER.save()
USER.displayName = await name
await USER.save()
USER.profilePic = await pic
await USER.save()
Related