【问题标题】:How to display nested list of lists with AngularFire2如何使用 AngularFire2 显示嵌套的列表列表
【发布时间】:2019-07-11 08:35:19
【问题描述】:

我正在尝试显示“站”列表,并在返回的每个“站”中显示当前位于该“站”的“项目”列表。使用 angularfire2 snapshotChanges,我可以轻松获取电台列表以及相关的元数据。然后我得到“stationName”并在另一个查询中使用它来获取与该站关联的项目。然后在嵌套的 ngFor 循环中,我显示了电台以及理想情况下相关的项目,但是它们没有显示出来,我不知道为什么。在控制台中,我可以看到在循环获取它们时关联的项目。我想我遗漏了一些明显的东西,任何帮助都会很棒。

想要的效果:

station 1
 - project 1, project 3, project 5

station 2
 - project 2, project 6

station 3
 - project 4, project 7, project 8

etc.

firestore 查询:

    this.shopsCollection = afs.collection('stations', ref => ref.orderBy('StationtName'));
    this.shops = this.shopsCollection.snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data() as Shop;
        const id = a.payload.doc.id;

        this.queues = this.afs.collection('projects', ref => ref.where('station', '==', data.StationtName)).valueChanges().subscribe(data=>{
          console.log(data);
         })
console.log(data.StationtName);
        return { id, ...data };
      }))

    );

html:

<ul>
    <li *ngFor="let shop of shops | async">
       <ion-button>{{shop.StationtName}}</ion-button>

            <div *ngFor="let queue of queues | async">
                 {{queue.SKU}}
            </div>  

    </li>
</ul> 

此特定设置会导致以下错误:

Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

非常感谢任何帮助。谢谢

【问题讨论】:

    标签: javascript angular firebase google-cloud-firestore angularfire2


    【解决方案1】:

    尝试删除 |队列 observable 上的异步管道,因为您已经在组件中订阅,您不需要在模板中做。

    HTH

    【讨论】:

    • 不,这给了我另一个错误,不记得是哪个。我认为问题在于我的 json 结构,我以某种方式以对象数组或数组数组结束。
    • 另一种方法是转换每个站点并添加一个对象,例如排队到每个车站(例如您的示例中的商店)。您可以通过地图做到这一点。这个网址对我实现这一点非常有帮助,并使代码更加精简angularfirebase.com/lessons/firestore-joins-similar-to-sql
    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多