Firebase orderByChild return the first time an array, the second time an objec

一笑奈何 提交于 2020-01-06 06:41:48

问题


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"
  }
}

来源:https://stackoverflow.com/questions/57069933/firebase-orderbychild-return-the-first-time-an-array-the-second-time-an-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!