angular fire querying-collections 文档示例代码不工作
angular fire querying-collections docs sample code not working
我正在阅读 angular 消防文档,特别是这个 https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md
示例代码下方的某处
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()
)
);
我从“this.items$”
得到这个错误
Type 'Observable<unknown[]>' is not assignable to type 'Observable<Item[]>'.
Type 'unknown[]' is not assignable to type 'Item[]'.
Type '{}' is missing the following properties from type 'Item': text, color,size(2322)
我错过了什么?
我正在使用 Visual Studio 代码,这些是 Angular 和 angular fire
的版本
Angular v10.0.4
@angular/fire@^6.0.2
几天来我一直在努力解决这个问题。希望有人能给个建议
您在 afs.colelction
中缺少通用类型,应该是 --> afs.collection<Item>
。这就是为什么它 returns 一个 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()
)
);
看来我是想多了,太相信文档了。感谢@sagat 再看一遍这个。 Angular Fire 文档需要更新。
文档应该是这样写的
afs.collection<Item>('items')
不打扰Visual Studio代码了
我正在阅读 angular 消防文档,特别是这个 https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md
示例代码下方的某处
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()
)
);
我从“this.items$”
得到这个错误Type 'Observable<unknown[]>' is not assignable to type 'Observable<Item[]>'. Type 'unknown[]' is not assignable to type 'Item[]'. Type '{}' is missing the following properties from type 'Item': text, color,size(2322)
我错过了什么?
我正在使用 Visual Studio 代码,这些是 Angular 和 angular fire
的版本Angular v10.0.4 @angular/fire@^6.0.2
几天来我一直在努力解决这个问题。希望有人能给个建议
您在 afs.colelction
中缺少通用类型,应该是 --> afs.collection<Item>
。这就是为什么它 returns 一个 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()
)
);
看来我是想多了,太相信文档了。感谢@sagat 再看一遍这个。 Angular Fire 文档需要更新。
文档应该是这样写的
afs.collection<Item>('items')
不打扰Visual Studio代码了