简体   繁体   English

angular 火灾查询集合文档示例代码不起作用

[英]angular fire querying-collections docs sample code not working

I was reading on the angular fire docs specifically this one https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md我正在阅读 angular 消防文档,特别是这个https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md

somewhere down below where the sample code is低于示例代码的地方

this.items$ = combineLatest(
  this.sizeFilter$,
  this.colorFilter$
).pipe(
  switchMap(([size, color]) => 
    afs.collection('items', ref => {
      let query : firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
      if (size) { query = query.where('size', '==', size) };
      if (color) { query = query.where('color', '==', color) };
      return query;
    }).valueChanges()
  )
);

I get this error from "this.items$"我从“this.items$”得到这个错误

Type 'Observable<unknown[]>' is not assignable to type 'Observable<Item[]>'.类型 'Observable<unknown[]>' 不可分配给类型 'Observable<Item[]>'。 Type 'unknown[]' is not assignable to type 'Item[]'.类型 'unknown[]' 不能分配给类型 'Item[]'。 Type '{}' is missing the following properties from type 'Item': text, color,size(2322)类型“{}”缺少类型“项目”的以下属性:文本、颜色、大小(2322)

What did I miss?我错过了什么?

I am using Visual Studio Code and these are the versions of Angular and angular fire我正在使用 Visual Studio Code,这些是 Angular 和 angular fire 的版本

Angular v10.0.4 @angular/fire@^6.0.2 Angular v10.0.4 @angular/fire@^6.0.2

I've been trying to wrap my head around this for a few days now.几天来,我一直在努力解决这个问题。 I hope someone can give an advice.希望有人能给点建议。

You are missing the Generic type in the afs.colelction , should be --> afs.collection<Item> .您在afs.colelction中缺少 Generic 类型,应该是 --> afs.collection<Item> Thats Why it returns an Unknown[] error.这就是它返回Unknown[]错误的原因。

this.items$ = combineLatest(
  this.sizeFilter$,
  this.colorFilter$
).pipe(
  switchMap(([size, color]) => 
    afs.collection<Item>('items', ref => { // generic type required here
      let query : firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
      if (size) { query = query.where('size', '==', size) };
      if (color) { query = query.where('color', '==', color) };
      return query;
    }).valueChanges()
  )
);

It seems I have been overthinking and over trusting the docs so much.看来我一直在想太多,也太信任文档了。 Thanks @sagat for another look on this one.感谢@sagat 再次查看这个。 The Angular Fire docs needs to be updated. Angular Fire 文档需要更新。

The docs should have wrote the code like this文档应该这样写代码

afs.collection<Item>('items')

It does not bother Visual Studio Code anymore.它不再打扰 Visual Studio Code。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM