【问题标题】:How can I get a collection from FireStore?如何从 FireStore 获取收藏?
【发布时间】:2019-05-17 21:24:04
【问题描述】:

我正在尝试从 Firestore 检索整个集合并按照说明逐步执行,但始终存在一些错误 map() 或 Observable 或 then() 不存在或不工作。

这就是我一直试图得到它的方式:


import { Injectable } from '@angular/core';
import { Employee } from './employee';
import { AngularFirestore } from '@angular/fire/firestore';

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {

  FormData: Employee;

  constructor(private firestore: AngularFirestore) { }

  getEmployees(){
    this.firestore.collection('employees').get().then((snapshot) => {
      snapshot.docs.forEach(doc => {
        console.log(doc);
      })
    })
  }
}


这就是我现在得到的:

src/app/employees/shared/employee.service.ts(16,50) 中的错误:错误 TS2339:“Observable”类型上不存在属性“then”。

【问题讨论】:

    标签: angular firebase google-cloud-firestore


    【解决方案1】:

    改用valueChanges() 尝试以下操作。 valueChanges() 返回一个 Observable 数据作为 JSON 对象的同步数组。

    getEmployees(){
      this.firestore.collection('employees')
        .valueChanges()
        .subscribe(docs => {
          // loop through each item in the array and log value
          docs.forEach(doc => console.log(doc));
        });
    }
    

    这来自valueChanges() 的文档。如果您需要 doc id,您可以将 snapshotChanges() 与 RxJS 可管道运算符(例如 map())结合使用(以创建包含 id 和数据的对象数组):

    getEmployees(){
      this.firestore.collection('employees')
        .snapshotChanges()
        .pipe(
           map(actions => actions.map(a => {
             const data = a.payload.doc.data();
             const id = a.payload.doc.id;
             return { id, ...data };
           });
        )
        .subscribe(docs => {
          // loop through each item in the array and log value
          docs.forEach(doc => console.log(doc.id));
        });
    } 
    

    理想情况下,您应该创建一个classinterface 来表示每个“员工”的数据结构并使用它来强类型化响应:

    服务:

    interface Employee {
      someProperty: number;
      anotherProperty: string;
      yetAnotherProperty: boolean;
    }
    
    // ...
    
    // perhaps return the observable so a component using this service can consume the data (can't return from inside subscribe())
    getEmployees(): Observable<Employee[]> {
      return this.firestore.collection<Employee>('employees').valueChanges();
    
      /* or instead snapshotChanges() with map()
        return this.firestore.collection<Employee>('employees')
          .snapshotChanges()
          .pipe(
            map(actions => actions.map(a => {
              const data = a.payload.doc.data();
              const id = a.payload.doc.id;
              return { id, ...data };
           });
        )
      */
    }
    

    组件:

    @Component({ /* ... */})
    export class SomeComponent {
      constructor(private employeeService: EmployeeService) {}
    
      ngOnInit() {
        this.employeeService.getEmployees().subscribe(employees => console.log(employees));
      }
    }
    

    希望对您有所帮助!

    【讨论】:

    • 我能用这种方式获取 doc 的 id 吗?因为我读到使用 valueChanges() 可以检索 json 文件但没有它们的 id。这是真的吗?
    • valueChanges(),不会返回 id,但文档表明 snapshotChanges() 会。您是否有机会阅读有关 @angular/fire 的任何文档?我会用一个例子来更新答案。
    • 是的,我已经浏览过了。刚刚进入 Firebase 的方式。如果它适用于其他人,我无法理解为什么会出现这些错误?
    • 该文档提供了很好的使用示例。与标准 Firestore JS 不同,@angular/fire 大量使用 RxJS 可观察对象,因此经常尝试做某事 then() 会导致错误。
    • 您的解决方案对我很有效?感谢您的帮助!现在我将尝试找出如何使用快照来做同样的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2018-04-18
    • 1970-01-01
    • 2021-01-08
    • 2018-10-18
    相关资源
    最近更新 更多