mongoose 查询对象无法读取问题
文章描述
mongoose 查询对象无法读取问题,mongoose 查询对象解构错误问题
const updateDetail = async (gameId, timer = false) => {
  const gameInfo = await DiceDuelModel.findOne({ gameId });
  if (!gameInfo) {
    socket.emit('error', `Games don't exist`);
    return;
  }
  let info = { ...gameInfo, timer };
  gameInfo.players.map(async(item) => {
    if (item.bot) {
      return;
    }
    if (loginClients[item.userId]) {
      loginClients[item.userId].emit('diceDuel getDetail', info);
    }
  })
}
上面代码用,info得到的值不是数据库正常的字段;其实是因为mongoose封闭了对象,导致无法读取问题,只要加上toOjbect()来转换即可
let info = { ...gameInfo.toObject(), timer };