【问题标题】:Cannot read property 'insertOnMatch' of undefined - TypeScript无法读取未定义的属性“insertOnMatch”-TypeScript
【发布时间】:2021-11-20 14:04:45
【问题描述】:

我正在使用 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)
  }

【问题讨论】:

    标签: javascript node.js typescript mongoose


    【解决方案1】:

    据我了解,您尝试使用 this 在功能组件上调用 insertOnMatch。它是未定义的,因为没有类实例。 accessing the 'this' keyword in a react functional component

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2018-03-13
      • 2018-08-30
      • 2017-11-02
      • 2018-01-21
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      相关资源
      最近更新 更多