我的 mongoDB 中有如下数据,我检索调用该服务并稍后订阅它。但我想在订阅之前对其进行过滤,以便我根据自己的情况订阅过滤后的数据库。 我想过滤验证后端数据中的阵营之一是否与“this.idcalbuscador”匹配。 (参见.filter())。我无法实现这一点,有人可以帮忙吗?刚刚接触可观察对象
数据
{
"_id" : ObjectId("588850b746f8ce140c1fe8cf"),
"idpertenalcalendario" : ObjectId("5885bfe452c6ba1d50f37f19"),
"enabled" : true,
"forma" : "Rombo",
"color" : "Red",
"type" : "point",
"descrip" : "ghghghgh",
"startDate" : "2017-01-15",
"nameHito" : "gjgjgjgg",
}
代码
//retrieve the datas
this._hitoService.getHitos()
.filter(resp => resp.idpertenalcalendario === this.idcalbuscador )
.subscribe(hito1 =>{
if (typeof this.nom_cal1 === "undefined"){
swal("Atencion!", "Busca un calendario con el buscador del Side Menu")
}else {
drawtimeline1_1(this.hito1, this.nom_cal1);
}
});
最佳答案
不会像 Java 的流 API 中那样为数组中的每个项目调用 filter
函数。它被称为 Observable 的单个发射率。这意味着您在过滤器函数中使用的 resp
包含数组,而不是单个 hito
。这就是为什么下面的比较总是返回 false:
resp.idpertenalcalendario === this.idcalbuscador
这至少是我根据名称所期望的:getHitos
返回一个 Hito
数组。但Pierre Duc是对的,这取决于你的实际实现。
关于javascript - 在订阅 angular2 之前过滤 Observables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41855300/