Cannot read property 'insertOnMatch' of undefined - TypeScript

Viewed 22

I'm using TypeScript to create a match/player module in mongoose. I create this insertOnMatch function to use the mongoose findOneAndUpdate function adding the player to my match schema, but I'm recieving a UnhandledPromiseRejectionWarning while trying to update that document in mongo

Full error code:

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

insertOnMatch function

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

create function

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 Answers
Related