【发布时间】:2018-06-24 03:00:28
【问题描述】:
我有以下函数来检查数据库中是否存在代码:
isCodeExist(code: string): Observable < boolean > {
return <Observable < boolean >> this.afs
.collection(this.jobsCollection, ref => ref
.where('code', '==', code)
.limit(1)
)
.valueChanges()
.pipe(
flatMap(jobs => jobs),
map(job => !!job)
);
}
这是我的使用方法:
onChangeCode() {
this.jobService.isCodeExist(this.job.code)
.pipe(
tap(isCode => {
console.log(isCode);
// I want to show an error message based on true or false returned
// this.showError = isCode;
})
)
.subscribe();
}
我想根据返回的 true 或 false 显示错误消息,但是 console.log 只记录 true。如果值为 false,则抽头部分永远不会被执行。因此,当存在现有代码但永远不会消失时,会出现错误消息。
更新:
我已经在pipe 中尝试过catchError(),但我也没有收到任何错误。
【问题讨论】: