I am attempting to include a User object in the "players_list" field of my Game object, which is essentially a list of User objects. Let's take a look at how my Game object is structured:
{ 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 }
Now, let's take a closer look at my User object:
{ games: [],
_id: 5b0e1820eb766e7bfaf2fb0b,
email: 'f',
password: '$2a$10$JmS.9axW8batMUKzE7OQx.GShdNDt09eArXfYGoI/DUWEKVwAn5ju',
handle: 'F',
__v: 0 }
In order to add the User object to the "players_list" field of the Game object, I execute
req.body.players_list.push(req.user)
. This updates the req.body with the new User object included in the player_list field of the Game object as shown below:
{ 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 }
However, upon updating the Game object in MongoDB using
Post.findByIdAndUpdate(req.params.id, req.body).then((result) => {...
, the resulting update does not reflect the addition of the new User. The updated result is depicted below:
{ 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 }
What caught my attention was that after navigating away from and returning to my current React Component (triggering fetchUser and fetchGame), the refreshed Game now includes the new User in its players_list. Could this be due to the asynchronous nature of the mongo update function? In any case, I assumed that the use of .then((result) => {
within
Post.findByIdAndUpdate(req.params.id, req.body).then((result) => {
would ensure that the execution sequence waited for Post.findByIdAndUpdate
to complete before proceeding.