【问题标题】:having problem in adding object key to the object generating asynchronously在将对象键添加到异步生成的对象时遇到问题
【发布时间】:2021-09-26 21:48:24
【问题描述】:

我从两个异步调用中获得了两个对象 runnningGame 和 runningGameTime。对象是:

    {
  numberOfBets: 0,
  totalAmountCollected: 0,
  amountDistributed: 0,
  finished: false,
  _id: 60f52b621bcef84190024cfd,
  gameId: 60f52b621bcef84190024cfb,
  allPlayerBettedBetId: [],
  createdAt: 2021-07-19T07:36:02.240Z,
  updatedAt: 2021-07-19T07:36:02.240Z,
  __v: 0
}
{
  startTime: 2021-07-19T07:36:02.235Z,
  _id: 60f52b621bcef84190024cfb,
  gameType: 'Slow Parity',
  endTime: 2021-07-19T07:39:02.235Z,
  createdAt: 2021-07-19T07:36:02.237Z,
  updatedAt: 2021-07-19T07:36:02.237Z,
  __v: 0
}

我正在做这些操作

try {
    let runningGame = await model.findOne({ finished: false });
    const runnigGameTime = await Game.findById(runningGame.gameId);
    const finishedGameDetails = await model.finishedGames();
    runningGame.endTime = finishedGameDetails.endTime;
    console.log(runningGame);
    res.send();
    // res.send({runningGameDetails:{endTime:runnigGameTime.endTime,startTime:runnigGameTime.startTime,runningGame},finishedGameDetails});
}
catch (error)
{
    console.log(error);
    res.status(400).send({ error });
}

但是在第 4 行的 try 块中没有执行我得到相同的 runningGame 对象,即使在将 endTime 添加到它之后也是如此。这里有什么问题?????????

【问题讨论】:

  • 请提供更多信息:您的modelmodel.finishedGames 是什么?
  • 不保存命令,为什么不是同一个对象?

标签: javascript object asynchronous mongoose


【解决方案1】:

这是因为 findOne() 返回一个 mongoose 文档模型。为了将 endTime 属性添加为普通的 JS 对象,可以使用 lean() 来完成。

let runningGame = await model.findOne({ finished: false }).lean();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-03
    • 2023-03-23
    • 2014-03-23
    • 2011-06-10
    • 2023-03-12
    • 2020-01-24
    • 1970-01-01
    相关资源
    最近更新 更多