Firebase orderByChild return the first time an arr

2019-08-30 08:03发布

问题:

I have the following database on firebase :

    {
      "pops" : [ {
        "description" : "22",
        "number" : "13",
        "serie" : "0",
        "title" : "luke skywalker [jedi]"
      }, {
        "description" : "Test",
        "number" : "55",
        "serie" : "2",
        "title" : "Bilbo"
      }, {
        "description" : "le second porteur d'anneau",
        "number" : "13",
        "serie" : "2",
        "title" : "Frodon"
      }],
      "series" : [ {
        "description" : "La grande saga des étoiles",
        "title" : "Star Wars"
      }, {
        "description" : "L'autre grande saga des étoiles",
        "title" : "Star Trek"
      }, {
        "description" : "Le seigneur des anneaux",
        "title" : "LOTR"
      } ]
    }

And I have the following component, which must display only "pops" from selected serie :

    export class SingleSerieComponent implements OnInit {

      serie: Serie;
      serieSubscription: Subscription;

      popsFromSerie: Pop[];
      popFromSerieSubscription: Subscription;
      constructorId: number;

      constructor(private route: ActivatedRoute, private serieService: SeriesService,
                  private router: Router,private popsService: PopsService) { 
        route.params.subscribe(val => {
          this.serie = new Serie(''); 
          const id = this.route.snapshot.params['id'];
          this.constructorId = id;
          this.serieService.getSingleSerie(+id).then(
            (serie: Serie) => {
              this.serie = serie;
            }
          );

          this.popsService.getPopsBySerieId2(this.constructorId);
          this.popFromSerieSubscription = this.popsService.popFromSerieSubject.subscribe(
            (pops : Pop[]) => {
              this.popsFromSerie = pops;
            }
          );
        });
      }
    }

The getPopsBySerieId2 method :

  getPopsBySerieId2(id: number){
    firebase.database().ref('/pops').orderByChild('serie').equalTo(id).on('value',(data: DataSnapshot)=>{
      this.popsFromSerie = data.val() ? data.val():[];
      this.emitPopsFromSerie();
      }
    );
  }

When I try to retrieve data for the serie with id '0', it return an array, the datas are displayed. But for any other datas, when the serie id is not equal to 0, the firebase method return an object.

Array response for the id 0, which display informations :

[
 {
   "description": "55",
   "number": "11",
   "serie": "0",
   "title": "boba fett"
 },
 {
   "description": "22",
   "number": "13",
   "serie": "0",
   "title": "luke skywalker [jedi]"
 }
]

Object response for another id :

{
  "9": {
    "description": "L'homme araignée",
    "number": "45",
    "serie": "4",
    "title": "Spiderman"
  },
  "10": {
    "description": "Cap sans son masque",
    "number": "45",
    "serie": "4",
    "title": "Steve Rogers"
  }
}