mongodb更新不会立即更新

时间:2018-05-30 03:34:00

标签: javascript database mongodb asynchronous mongoose

我正在尝试将User对象添加到我的Game对象的字段“players_list”,这是一个User对象列表。这就是我的Game对象的样子:

{ 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 }

这就是我的用户对象的样子

{ games: [],
  _id: 5b0e1820eb766e7bfaf2fb0b,
  email: 'f',
  password: '$2a$10$JmS.9axW8batMUKzE7OQx.GShdNDt09eArXfYGoI/DUWEKVwAn5ju',
  handle: 'F',
  __v: 0 }

然后我运行req.body.players_list.push(req.user)将User对象添加到Game对象的player_list字段中。这是添加User对象后req.body的样子:

{ 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 }

因此,我使用Post.findByIdAndUpdate(req.params.id, req.body).then((result) => {...更新mongo中的Game对象,但结果不包含新用户。这就是我的结果

{ 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 }

奇怪的是,如果我离开当前的React组件,然后返回它(从而触发fetchUser和fetchGame),那么获取的游戏确实在其players_list中有新用户。这是否因为mongo更新函数是异步的?即便如此,我认为.then((result) => {中的Post.findByIdAndUpdate(req.params.id, req.body).then((result) => {确保Post.findByIdAndUpdate在继续之前先完成

1 个答案:

答案 0 :(得分:1)

如果您查看文档:{​​{3}},您会注意到findByIdAndUpdate返回原始对象,而不是更新的文档。您必须传递{new: true}作为选项才能使用更新后的对象进行解析。