0

I have data like the one below in my mongoDB and I retrieve calling the service and later subscribing to it. But I want to filter the before subcribing to it in a way that i just subscribe to the filtered data base on my condition. I want to filtered verifying that one of the camps in the data from the backend matches the "this.idcalbuscador". (See the .filter()). I haven't been able to achieve this, could anyone help? Just new to observables

Data

{
    "_id" : ObjectId("588850b746f8ce140c1fe8cf"),
    "idpertenalcalendario" : ObjectId("5885bfe452c6ba1d50f37f19"),
    "enabled" : true,
    "forma" : "Rombo",
    "color" : "Red",
    "type" : "point",
    "descrip" : "ghghghgh",
    "startDate" : "2017-01-15",
    "nameHito" : "gjgjgjgg",
}

Code

//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);
        }
    });
GillesC
  • 10,647
  • 3
  • 40
  • 55

1 Answers1

2

The filter function is not called for every item in your array, like it is for instance in Java's stream API. It is called for a single emittance of the Observable. That means the resp you work with in the filter function contains the array and not a single hito. That's why the following comparison will always return false:

resp.idpertenalcalendario === this.idcalbuscador

This is, at least, what I'd expect according to the names: getHitos returns an array of Hito. But Pierre Duc is right, it depends on your actual implementation.

Jan B.
  • 6,030
  • 5
  • 32
  • 53