angular - 类型 'AngularFireAction<DatabaseSnapshot<{}>>[]' 不可分配给类型 'Product[]'

标签 angular firebase angularfire2 angularfire

所以我正在学习这门类(class),它使用的是以前版本 (4.0) 的 angularfire2,我使用的是最新版本 (5.0),但我的代码遇到了这个问题。

我收到此错误,Type 'AngularFireAction>[]' is not assignable to type 'Product[]'。

export class AdminProductsComponent implements OnInit, OnDestroy {
products: Product[];
filteredProducts: any[];
subscription: Subscription;
tableResource: DataTableResource<Product>;
items: Product[];
itemCount: number;

constructor(private productService: ProductService) {
 this.subscription = this.productService.getAll().subscribe(products => {
  this.filteredProducts = this.products = products;
  this.initializeTable(products);
 });

}

private initializeTable(products: Product[]){
 this.tableResource = new DataTableResource(products);
  this.tableResource.query({ offset: 0})
    .then(items => this.items = items);
  this.tableResource.count()
    .then(count => this.itemCount = count);
}

reloadItems(params){ 
 if(!this.tableResource) return;
 this.tableResource.query(params)
    .then(items => this.items = items);
}

filter(query: string){
   this.filteredProducts = (query) ?
   this.products.filter(p => 
   p.title.toLowerCase().includes(query.toLowerCase())) :
   this.products; 
}

ngOnDestroy(){
  this.subscription.unsubscribe();
}


}

这里是产品服务的代码

export class ProductService {

constructor(private db: AngularFireDatabase) { }

create(product){
  return this.db.list('/products').push(product);
}

getAll(){
  return this.db.list('/products').snapshotChanges(); 
}

get(productId){
  return this.db.object('/products/' + productId);
}

update(productId, product){
  return this.db.object('/products/' + productId).update(product);
}

delete(productId){
  return this.db.object('/products/' + productId).remove(); 
}

最佳答案

getAll()您服务中的方法正在返回 snapshotChanges() .这是一个Observable<AngularFireAction<DatabaseSnapshot<{}>>[]>你正试图将其传递给 initializeTable(products: Product[]) .这就是错误的意思。

为了解决这个问题,你需要映射.snapshotChanges()给你的Product[]像这样:

getAll(): Observable<Product[]> {
    return this.db.list<Product>('/products')
        .snapshotChanges()
        .pipe(
            map(changes =>
                changes.map(c => {
                    const data = c.payload.val() as Product;
                    const id = c.payload.key;
                    return { id, ...data };
                })
            )
        );
}

关于angular - 类型 'AngularFireAction<DatabaseSnapshot<{}>>[]' 不可分配给类型 'Product[]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51741814/

相关文章:

typescript - 类型 'take' 上不存在属性 'FirebaseObjectObservable<any>'

angular - 将 ng2-charts 添加到 Angular 7 应用程序

文档中的 Angular FireStore 集合

android - 我有适配器问题。任何人都可以知道解决方案

firebase - 是否有用于 Google Firebase 集成的 Cloud Firestore 的任何 BI 软件

javascript - 如何在 Firestore 中查询 DocumentReference

javascript - 将 Javascript 库 (jsencrypt) 导入 Angular 2 应用程序

html - Angular 5 : Spinner is not working perfectly on page load

重启并启用持久性后的 firebase 事务

angular - 使用 Angular2、angularfire2 和 Typescript 从 Firebase 对象中获取数据