Subscribe to multiple observations

I need to subscribe several times to get the correct data from my NoSQL database. To get a list of users for a specific project, I do the following:

ngOnInit() {
    //Subscription 1 (Service): Get project data
    this.projectService.getCurrentproject().take(1).subscribe(projectData => {
        console.log('Got project data.');
        //Subscription 2: Get project user IDS
        this.af.database.list('/project_roles/'+projectData.$key)
            .subscribe((userMeta) => {
                });
            });
}
  • As you can see, the subscription is inside the subscription inside the subscription .. And each subscription depends on the previous reslt.

  • Subscription 3 and 4 may be parallel.

The code works well, but am I missing something, or so I believe that I work with several subscriptions, that the result of the previous one depends on the following?

Update . It looks like the issue is with Subscription 2. The subscription is not finished yet, but I'm starting to iterate over the list, which gives me a double list of users!

+4
1

.

ngOnInit() {
    this.projectService.getCurrentproject().take(1)
        .flatMap(projectData => {
            return this.af.database.list('/project_roles/'+projectData.$key)
        })
        .flatMap(userMeta => {
            let subs = userMeta.map(projectRole => {
                return Observable.zip([
                    this.af.database.object('/roles/'+projectRole.$value),
                    this.af.database.object('/users/'+projectRole.$key)
                ]).map(([roleData, user]) => {
                    user.role = roleData.$value;
                    return Observable.of(user)
                })
            });
            return Observable.zip(subs)
        })
        .subscribe(users => {
            this.users = users
        })
}

, ,

0

Source: https://habr.com/ru/post/1658307/


All Articles