首页 > 解决方案 > 了解 Rxjs 可观察对象

问题描述

我对 RXJS 还很陌生

我很感激帮助理解为什么下面的代码返回一个Observable<Observable<any[]>>而不是Observable<any[]>

 getResults() {
        // first endpoint to get the logged-in contact's id
        return this.getContact()
            .pipe(
                switchMap((contact: any) => { 
                    // second endpoint to get a contact's subordinates
                    return this.getSubordinates(contact[0].EntityId)
                        .pipe(
                            map((subs: any[]) => {
                                // if contact has subs, it means he or she are a supervisor
                                if (contacts.length) {
                                    // get details of subs
                                    var obsArr = contacts.map(c => {
                                        // third endpoint to get a contact's details
                                        return this.getDetailsOfContact(c.EntityId).pipe(
                                            map(details => ({ details , c }))
                                        );
                                    })
                                    // also get details of supervisor itself
                                    var supervisorObs = this.getDetailsOfContact(contact.EntityId).pipe(
                                        map(details=> (
                                            {
                                                details,
                                                c: {
                                                    contactid: contact.Attributes.contactid,
                                                    fullname: contact.Attributes.fullname
                                                }
                                            }
                                        )
                                        )
                                    )
                                    obsArr.push(supervisorObs)
                                    return obsArr;
                                } else {
                                    // if contact does not have any subordinates
                                    // it means they are not a supervisor
                                    // then get details for supervisor only
                                    var obsArr1: Observable<any>[] = [];
                                    obsArr1.push(this.getDetailsOfContact(contact.EntityId).pipe(
                                        map(details => (
                                            {
                                                details,
                                                c: {
                                                    contactid: contact.Attributes.contactid,
                                                    fullname: contact.Attributes.fullname
                                                }
                                            }
                                        )
                                        )
                                    ));
                                    return obsArr1;
                                }
                            }
                            )
                        );
                })
            )
    }

最终,我希望getResults()返回一个 Observable,它发出一个联系人数组。单个联系人(如果他们不是主管或如果他们是主管但没有任何下属,则为登录的联系人)或多个联系人(如果他们是主管并且有下属)

标签: angularrxjs

解决方案


this.getContact() 是一个 observable,getSubordinates() 也是一个 observable。getSubordinates() 是一个内部可观察对象。所以输出是正确的。


推荐阅读