javascript - 如何处理 Angular 流中多个位置的捕获错误?
问题描述
现在,我正在尝试确保在首先获取组 id 的流中正确处理错误,然后使用该组 id 获取配置文件信息。
我需要它,所以很明显哪个步骤导致了错误——要么是获取 groupId,要么是获取配置文件信息。
现在我有这样的东西,但我不确定这是否正确。
this.groupRepository
.getUserGroup()
.pipe(mergeMap(group) => {
return this.profileRepository.getAllProfiles(group.id)
})
.subscribe(
(res) => {
// doing things in here to set the groups and profiles
},
(error) => {
this.error = error;
}
);
解决方案
你做对了。
两种方法的错误最终都会出现在(error) => {...}
方法中。
小测试:
of('A', 'B', 'C').pipe(mergeMap(letter => {
return of('E', 'F', 'G');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
返回:'E'、'F'、'G'、'E'、'F'、'G'
使用第二种方法抛出:
of('A', 'B', 'C').pipe(mergeMap(letter => {
return throwError('bad');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
返回:“坏”
使用第一种方法抛出:
throwError('bad').pipe(mergeMap(letter => {
return of('E', 'F', 'G');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
返回:“坏”
推荐阅读
- javascript - 使用此特定代码使用 JavaScript 在实际网页上打印一些消息的最简单方法
- bayesian - 错误消息切片器卡在具有无限密度的值
- java - 使用 Spring 集成的 Java 中的 Python。错误:javax.script.ScriptEngineManager 无法为语言“python”创建脚本引擎
- java - 数组中的递增或递减序列
- c# - 如何在他们的示例 Pet Store 的 swagger.io csharp 客户端中配置 nunit 测试?
- javascript - 从 Chrome 扩展发送 POST 请求
- linux - Ansible - winrm 或 requests 未安装
- javascript - 如何从 onChange() 事件中获取道具?
- flutter - 如何使用 StreamBuilder 检查我们是否在 ListView 的末尾
- android - Unity Google Play Console Warning optimize APK