无法读取未定义的属性 'insertOnMatch' - TypeScript

3

我正在使用TypeScript在mongoose中创建一个匹配/玩家模块。

我创建了这个insertOnMatch函数来使用mongoose findOneAndUpdate函数将玩家添加到我的匹配模式中,但是当尝试更新mongo文档时,我收到了一个UnhandledPromiseRejectionWarning警告。

完整的错误代码:

(node:508) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'insertOnMatch' of undefined

insertOnMatch函数

  private async insertOnMatch (matchId: String, playerId: String): Promise<void> {
    await Match.findOneAndUpdate({ matchId: matchId }, { $push: { matchPlayers: { playerId: playerId, playerRefererMatchId: matchId } } })
  }

create函数

public async create (req: Request, res: Response): Promise<Response> {
    const newPlayerConfig = {
      playerId: generatedPlayerId,
      playerMatchRefererId: req.params.matchId
    }

    const newPlayer = await Player.create(newPlayerConfig)

    this.insertOnMatch(newPlayer.playerMatchRefererId, newPlayer.playerId).catch(err => { console.error(err) })

    return res.json(newPlayer)
  }
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接