Angular 如何从FireStore获得收藏?

Angular 如何从FireStore获得收藏?,angular,firebase,google-cloud-firestore,Angular,Firebase,Google Cloud Firestore,我试图从Firestore中检索整个集合,并一步一步地遵循说明,但始终存在一些错误,map()或,Observable或then()不存在或不起作用 这就是我一直试图得到它的方式: import { Injectable } from '@angular/core'; import { Employee } from './employee'; import { AngularFirestore } from '@angular/fire/firestore'; @Injectable({

我试图从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”。

请使用
valueChanges()尝试以下操作
valueChanges()
将可观察到的数据作为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));
    });
}
这来自于的文档。如果需要记录id,可以与RxJS可管道操作符(如
map()
)结合使用(以创建包含id和数据的对象数组):

理想情况下,您应该创建一个
接口
,以表示每个“员工”的数据结构,并使用该数据结构强烈键入响应:

服务:

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 };
       });
    )
  */
}

希望这有帮助

我能用这种方法抓到医生的身份证吗?因为我读到使用valueChanges()可以检索json文件,但不需要它们的ID。这是真的吗?
valueChanges()
,将不会返回id,但文档表明
snapshotChanges()
将返回id。您是否有机会阅读有关@angular/fire的文档?我会用一个例子来更新答案。是的,我已经略过了。只是想插手Firebase的事情。如果它对其他人有效,我无法理解为什么会出现这些错误。文档提供了很好的使用示例。与标准Firestore JS不同,@angular/fire大量使用RxJS可观测值,因此经常尝试执行
操作,然后()
将导致错误。您的解决方案对我来说非常有效
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));
  }
}