Reputation: 161
I am trying to add an User object to my Game object's field "players_list", which is a list of User objects. This is what my Game object looks like:
{ players_list:
[ { games: [Array],
_id: '5b0e112ff13033792f08566f',
email: 'c',
password: '$2a$10$iWOBvVf4KAPwbH7zDczfYeI5iXI721jQ7bN1juJ4Us3R.Lqetmhfu',
handle: 'C',
__v: 0,
id: '5b0e112ff13033792f08566f' } ],
_id: '5b0e181aeb766e7bfaf2fb09',
players_status:
[ { _id: '5b0e181aeb766e7bfaf2fb0a',
playerId: '5b0e112ff13033792f08566f',
status: 'Joined' } ],
postGameEvaluation: [],
date: 'QQQQ',
time: 'QQQQ',
duration: 4,
players_needed: 4,
max_players: 4,
level: 4,
author:
{ games:
[ '5b0e13e69d35007a147578da',
'5b0e15b4b117987b00d68cb4',
'5b0e181aeb766e7bfaf2fb09' ],
_id: '5b0e112ff13033792f08566f',
email: 'c',
password: '$2a$10$iWOBvVf4KAPwbH7zDczfYeI5iXI721jQ7bN1juJ4Us3R.Lqetmhfu',
handle: 'C',
__v: 0,
id: '5b0e112ff13033792f08566f' },
__v: 0 }
and this is what my User objet looks like
{ games: [],
_id: 5b0e1820eb766e7bfaf2fb0b,
email: 'f',
password: '$2a$10$JmS.9axW8batMUKzE7OQx.GShdNDt09eArXfYGoI/DUWEKVwAn5ju',
handle: 'F',
__v: 0 }
I then run req.body.players_list.push(req.user)
to add the User object to the player_list field of the Game object. This is what by req.body looks like after adding the User object:
{ players_list:
[ { games: [Array],
_id: '5b0e112ff13033792f08566f',
email: 'c',
password: '$2a$10$iWOBvVf4KAPwbH7zDczfYeI5iXI721jQ7bN1juJ4Us3R.Lqetmhfu',
handle: 'C',
__v: 0,
id: '5b0e112ff13033792f08566f' },
{ games: [],
_id: 5b0e1820eb766e7bfaf2fb0b,
email: 'f',
password: '$2a$10$JmS.9axW8batMUKzE7OQx.GShdNDt09eArXfYGoI/DUWEKVwAn5ju',
handle: 'F',
__v: 0 } ],
_id: '5b0e181aeb766e7bfaf2fb09',
players_status:
[ { _id: '5b0e181aeb766e7bfaf2fb0a',
playerId: '5b0e112ff13033792f08566f',
status: 'Joined' } ],
postGameEvaluation: [],
date: 'QQQQ',
time: 'QQQQ',
duration: 4,
players_needed: 4,
max_players: 4,
level: 4,
author:
{ games:
[ '5b0e13e69d35007a147578da',
'5b0e15b4b117987b00d68cb4',
'5b0e181aeb766e7bfaf2fb09' ],
_id: '5b0e112ff13033792f08566f',
email: 'c',
password: '$2a$10$iWOBvVf4KAPwbH7zDczfYeI5iXI721jQ7bN1juJ4Us3R.Lqetmhfu',
handle: 'C',
__v: 0,
id: '5b0e112ff13033792f08566f' },
__v: 0 }
So, I then update the Game object in mongo with Post.findByIdAndUpdate(req.params.id, req.body).then((result) => {...
, but the result doesn't contain the new User. This is what my result looks like
{ players_list: [ 5b0e112ff13033792f08566f ],
_id: 5b0e181aeb766e7bfaf2fb09,
players_status:
[ { _id: 5b0e181aeb766e7bfaf2fb0a,
playerId: '5b0e112ff13033792f08566f',
status: 'Joined' } ],
postGameEvaluation: [],
date: 'QQQQ',
time: 'QQQQ',
duration: 4,
players_needed: 4,
max_players: 4,
level: 4,
author: 5b0e112ff13033792f08566f,
__v: 0 }
The strange thing is that if I leave my current React Component, and then go back into it (thus triggering fetchUser and fetchGame), then the Game fetched does have the new User in its players_list. Does this happen because the mongo update function is asynchronous? Even so, I thought that the .then((result) => {
in Post.findByIdAndUpdate(req.params.id, req.body).then((result) => {
made sure that Post.findByIdAndUpdate
completed first before continuing
Upvotes: 0
Views: 402
Reputation: 341
If you look at the docs: http://mongoosejs.com/docs/api.html#findbyidandupdate_findByIdAndUpdate you'll notice that findByIdAndUpdate returns the original object, not the updated doc. You have to pass in {new: true}
as options for it to resolve with the updated object.
Upvotes: 1