首页 文章

在订阅angular2之前过滤Observable

提问于
浏览
0

我的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);
        }
    });

1 回答

  • 2

    不为数组中的每个项调用 filter 函数,例如在Java的流API中 . 它被称为Observable的单个发射率 . 这意味着你在filter函数中使用的 resp 包含数组而不是单个 hito . 这就是为什么以下比较将始终返回false:

    resp.idpertenalcalendario === this.idcalbuscador
    

    这至少是我根据名称所期望的: getHitos 返回一个 Hito 数组 . 但皮埃尔·杜克是对的,这取决于你的实际实施情况 .

相关问题