如何使用 AngularFire2 和 Firebase 加入两个节点? [英] How to JOIN two nodes with AngularFire2 and Firebase?

查看:26
本文介绍了如何使用 AngularFire2 和 Firebase 加入两个节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Firebase 数据库具有这种结构

I have this structure of my Firebase Database

每个库可以有多个专辑.如何显示每个库的专辑?

Each library can have several albums. How can I display the albums for each library?

albumsPerLib: 第一个元素是库密钥,在每个库密钥下面,我存储属于它的专辑密钥.

the albumsPerLib: first element is the library key, and underneath each library key, I store the albums keys, that belong to it.

但我不习惯 NoSQL,不知道如何加入表格来显示每个图书馆的专辑.

But I'm not used to NoSQL and can't figure how to join the tables to display the albums for each library.

推荐答案

所以这有效:

  getAlbumsThatBelongToLib(libKey:string):Observable<Album[]>{
//get the keys for the lib albums
const albumsPerLib$ = this.db.list(`albumsPerLib/${libKey}`);

//map the returned list of keys
//and get their details from the albums node
return albumsPerLib$
  .map((albumKeys) => albumKeys
      .map( (albumKey) => {
         return this.db.object(`albums/${albumKey.$key}`)
      }))
  .flatMap((res) => {
    return Observable.combineLatest(res);
  });
}

这篇关于如何使用 AngularFire2 和 Firebase 加入两个节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆