简体   繁体   中英

how to get data from firebase as array of key that array of item in ionic 3

I have been dealing with a problem for several hours already. I'm trying to do the next thing:

I need to get a list of lessons, the lessons are defined according to sets of lessons, and I need an array of all the lessons belonging to that person.

This is how the structure looks at firebase:

在此处输入图片说明

I really messed up, tried to write whatever I thought, using MergeMap, and the like. But it did not help me. please help me.

My code looks like this, I have a provider that should return my list of lessons. But I could not move beyond single mergeMap .

At the lessonService:

getLessons(user: User): Observable<any[]>{
    return this.database.list(`/lessons/${user.uid}`)
    .mergeMap(key => this.database.list(`/lessons/${user.uid}/${key}`));
}

At the LessonPage:

private lessons: Lesson[];

ngOnInit(): void {
    this.loader.present();
    this.lessonService.getLessons(this.authenticatedUser)
    .subscribe(_setLessonSet => {
        _setLessonSet.forEach(set => {
            set.forEach(lessonItem => {
                this.lessons.push(lessonItem);
            });
        });
        this.loader.dismiss();
    });
}

And I'v got this error:

ERROR Error: Reference.child failed: First argument was an invalid path = "/lessons/eOqeJyTmQqQKa1bdzorMEDle4qD3/[object Object],[object Object]". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

Thanks for helpers!

I found the answer

getLessonSets(): Observable<LessonSet[]>{
if(!this.authenticatedUser) return new EmptyObservable<LessonSet[]>();
return this.database.list(`/lessonsLists/${this.authenticatedUser.uid}`);}

and loop of this, like:

this.LessonSets = this.lessonService.getLessonSets();
this.lessonSetsArray = [];
this.LessonSets.forEach(LessonsetItem => {
  LessonsetItem.forEach(OneLessonSet => {
    this.lessonSetsArray.push(OneLessonSet);
  });
});

and in the View (html file) iterate of this by async pip

*ngFor="let LessonsetItem of LessonSets | async

thank any way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM