简体   繁体   English

从 Angularfire2 返回值

[英]Returing value from Angularfire2

I'm getting crazy with this, so I submit to the audience.我对此感到很疯狂,所以我向观众屈服。

I need to return an Observable.我需要返回一个 Observable。 I first call Firestore to retrieve a field that contains an array and then based on each value of the array, I want to retrieve the matching key.我首先调用 Firestore 来检索一个包含数组的字段,然后根据数组的每个值,我想检索匹配的键。 Fairly easy usually.通常相当容易。

I see exactly what I cant on the console, so data are here, but why the hell they never show up in the view.我在控制台上看到了我看不到的东西,所以数据在这里,但为什么它们从未出现在视图中。 Before I gave up, I made countless variations of the below.在我放弃之前,我对以下内容进行了无数次修改。

My Service – Edited to reflect suggestions.我的服务 - 编辑以反映建议。 Still not working还是行不通

    getUserActivites() {

    console.log(this.userId)
    let array = []

    const agenda = this.afs.doc(`users/${this.userId}`).snapshotChanges()

     agenda.subscribe(list => {
      const data = list.payload.data().activities
       data.map(event => {
        const id = event
        const dataRef = this.afs.doc(`activities/${event}`).valueChanges()
         dataRef.subscribe(date => {
           array.push(date)
           console.log(array) //array log the expected data in an array of Object
          return data 
        })
      })
    })
    return agenda
  }

My Component public agendaData: Observable<any>我的组件public agendaData: Observable<any>

then in constructor然后在构造函数中

this.agendaData = this.agenda.getUserActivites()

My View我的看法

<div *ngFor="let item of agendaData | async ">
{{ item.name }}
</div>

1) I think you forgot to subscribe() in this.afs.....snapshotChanges() . 1)我想你忘了subscribe()this.afs.....snapshotChanges() One of the differences between Promise and Observables is that Observables is lazy - you need to subscribe it to start working. Promise 和 Observables 的区别之一是 Observables 是惰性的——你需要订阅它才能开始工作。

2) If you do not need key/id you should use valueChanges() that is easier to use. 2) 如果您不需要 key/id,则应使用更易于使用的valueChanges() Example (not tested):示例(未测试):

getUserActivites() {
    let agenda = [] //Created for logging only

    let usersTask = this.afs.object(`users/${this.userId}`).valueChanges();     
    let myReturnTask = usersTask.subscribe(users => {           
        console.log('received users');
        users.map(user => {             
            const dataRef = this.afs.doc(`activities/${user.id}`).valueChanges()

            dataRef.subscribe(date => {
              agenda.push(date) //For logging purpose I created this array
              console.log(agenda) //Here I see exactly what I want!

              return date  // Why date is not returned?
            }) 
        })
    })

    return myReturnTask;
}

Does getUserActivities = () => { work? getUserActivities = () => { 工作吗?

Is date an observable?日期是可观察的吗?

this.observableBooks = this.bookService.getBooksWithObservable(); this.observableBooks = this.bookService.getBooksWithObservable();

getBooksWithObservable(): Observable { return this.http.get(this.url).map((res: Response) => res.json()); getBooksWithObservable(): Observable { return this.http.get(this.url).map((res: Response) => res.json());

Sorry if it doesn't answer your question I like to know myself what the issue is here!抱歉,如果它不能回答您的问题,我想知道自己的问题是什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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