【发布时间】: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