从Firebase集合中获取类别名称,并将其放入另一个集合中

时间:2018-10-24 14:19:42

标签: javascript angular firebase google-cloud-firestore

我有类似的收藏集:

categories (collection):
--id1 (ID od document)
----name: "Sport"
--id2
----name: "Hobby"

books (collection):
--id1 (ID od document)
----categories: [id1, id2]
----name: "Book title 1"
--id2
----categories: [id1]
----name: "Book title 2"

我需要获取可以使用类别名称而不是类别ID的书籍列表:

books = [{
  categories: ['Sport', 'Hobby'],
  name: "Book title 1"
}, {
  categories: ['Sport'],
  name: "Book title 2"
}]

我不知道如何获取类别名称并将其放入书籍文档中。请在下面查看我的代码:

public getBooks(): Observable<any> {
return this.db
  .collection('books')
  .snapshotChanges()
  .pipe(
    map(books => {
      return books.map(book => {
        const transformedCategories = book.payload.doc.data()['categories'].map(categoryId => {
          return this.db.collection('categories').doc(categoryId).snapshotChanges().pipe(
            map(changes => {
              return changes.payload.data()['name'];
            }),
          );
        });
        return {
          id: book.payload.doc.id,
          title: word.payload.doc.data()['title'],
          categories: ???
        };
      });
    })
  );
}

0 个答案:

没有答案