多个javascript承诺。然后和。catch命中错误的catch
我认为我没有正确理解这里的某些东西,因为这是一种非常奇怪的行为。如果我调用queryFindPlayer,它应该会落入。如果queryFindContract函数不存在,它会这样做,但当它存在时,如下图所示,它似乎会落入queryFindPlayer捕获并添加一个新的播放器多个javascript承诺。然后和。catch命中错误的catch,javascript,node.js,Javascript,Node.js,我认为我没有正确理解这里的某些东西,因为这是一种非常奇怪的行为。如果我调用queryFindPlayer,它应该会落入。如果queryFindContract函数不存在,它会这样做,但当它存在时,如下图所示,它似乎会落入queryFindPlayer捕获并添加一个新的播放器 queryFindPlayer(models, ConsoleId, UserId, SeasonId, LeagueId).then(players => { const player = players[0
queryFindPlayer(models, ConsoleId, UserId, SeasonId, LeagueId).then(players => {
const player = players[0];
queryFindContract(db, player.Team.id, UserId, SeasonId, LeagueId).then(contracts => {
console.log("player has a contract to a team");
}).catch(e => {
console.log("failed to find player");
});
}).catch(e => {
queryAddPlayer(models, UserId, TeamId).then(player => {
console.log("added player");
}).catch(addPlayerError => {
console.log("failed to add player, shouldn't happen");
});
});
如果queryfindPlayer()
解析为开始执行.then()
处理程序,但最终进入queryfindPlayer().catch()
处理程序,则可能会出现以下原因之一:
queryFindContract()
之前抛出异常,例如Ifconst player=players[0]
抛出错误,则未定义IfqueryFindContract()
queryFindContract()
,例如player.Team.id
抛出,或者您传递的任何其他变量都不存在
queryFindContract()
在返回其承诺之前同步抛出
queryFindContract()
不返回承诺,因此queryFindContract()。那么()
将抛出异常
queryFindPlayer.then()
处理程序中引发同步异常,这将导致它转到queryFindPlayer.catch()
处理程序。它从未到达queryFindContract().catch()
处理程序,因为queryFindContract()
要么从未执行,要么从未完成并返回其承诺
您很可能只需添加以下内容就可以准确地看到造成您的情况的原因
console.log(e)
在两个.catch()
处理程序的开头。为清楚起见,还应在e
之前添加描述性字符串。例如:
console.log("qfc", e);
及
我几乎总是记录拒绝,即使有时可能会出现拒绝,因为您也可能因为编程错误等意外原因而收到拒绝,并且您希望能够立即看到这些拒绝,而不会被它们弄糊涂。感谢您的帮助,我并没有按照预期使用异常处理程序
queryGetContractById(models, id).then(c => {
return queryFindPlayer(models, ConsoleId, UserId, SeasonId, LeagueId).then(players => {
if(players.length != 0) {
return queryFindContract(models, players[0].Team.id, UserId, SeasonId, LeagueId).then(contracts => {
if(contracts.length != 0) {
console.log("player has a contract to a team");
} else {
queryUpdatePlayersTeam(models, players[0].id, TeamId);
}
});
} else {
return queryAddPlayer(models, UserId, TeamId).then(player => {
console.log("added player");
});
}
});
}).catch(e => {
console.log("failed", e);
});
在我的承诺中,如果没有返回数据,我会拒绝,如果有,我会解决。我知道这是怎么回事,我认为现在是对的。您必须显示
queryFindContract()
和queryAddPlayer()
的代码。此外,您可能会从.then()
或.catch()
handlers.FYI中返回嵌套的承诺,它将从queryFindContract()
返回到第二个.catch()
,如果queryFindContract()
引发同步异常,或者参数中存在导致异常的其他内容,或者queryFindContract()
未返回承诺。只需将console.log(e)
放在.catch()
两个处理程序的开头,您就可以准确地看到出了什么问题。我几乎总是记录拒绝,因为有时会遇到意外的拒绝,您需要知道它们是什么,以查看它们是否是编程错误而不仅仅是数据错误。如果它进入拒绝处理程序(您传递的fncatch
),这意味着a)queryFindPlayer
拒绝其承诺,或者B)在您的实现处理程序中抛出错误(您传递的fn然后作为其第一个参数)。根据您的描述,听起来像是queryFindPlayer
正在履行其承诺,但players[0]
是未定义的
,因此player.Team
抛出了一个错误。一个名为queryFindPlayer
(单数)的函数返回一个播放器数组(复数)似乎很奇怪。基本上,您需要使用调试器来找出发生了什么。旁注:当您找到播放机,但没有找到他们的合约时,记录“未能找到播放机”似乎很奇怪。如果在创建并返回queryFindContract()
承诺之前发生错误,则不会进入自己的。catch()
。然后,它将是顶级.Then()
中的异常,这将导致流转到顶级.catch()
。记录你的错误,一切都会变得清晰。@jwood-如果这有助于你解决问题,那么你可以通过单击答案左侧的复选标记向社区表明这一点,这也会为你在这里遵循正确的程序赢得一些声誉点数。
queryGetContractById(models, id).then(c => {
return queryFindPlayer(models, ConsoleId, UserId, SeasonId, LeagueId).then(players => {
if(players.length != 0) {
return queryFindContract(models, players[0].Team.id, UserId, SeasonId, LeagueId).then(contracts => {
if(contracts.length != 0) {
console.log("player has a contract to a team");
} else {
queryUpdatePlayersTeam(models, players[0].id, TeamId);
}
});
} else {
return queryAddPlayer(models, UserId, TeamId).then(player => {
console.log("added player");
});
}
});
}).catch(e => {
console.log("failed", e);
});