我有下面的代码段在Angular 6中不起作用。编译器显示错误,因为内部map函数的类型为“ {}”的属性“ map”不存在”。有人可以指导我如何从快照更改中获取有效负载和ID。
代码:
this.postCollection.snapshotChanges().pipe(
map(actions => actions.map(a => { //this inner map throws error >Property 'map' does not exist on type '{}'
const data = a.payload.doc.data() as Post;
const id = a.payload.doc.id;
return { id, ...data };
}))
);
依赖性:
"dependencies": {
"@angular/animations": "^6.0.0",
"@angular/cdk": "^6.3.1",
"@angular/common": "^6.0.0",
"@angular/compiler": "^6.0.0",
"@angular/core": "^6.0.0",
"@angular/forms": "^6.0.0",
"@angular/http": "^6.0.0",
"@angular/material": "^6.3.1",
"@angular/platform-browser": "^6.0.0",
"@angular/platform-browser-dynamic": "^6.0.0",
"@angular/router": "^6.0.0",
"angularfire2": "^5.0.0-rc.11",
"core-js": "^2.5.4",
"firebase": "^5.1.0",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
谢谢。
答案 0 :(得分:1)
编辑真实答案
我发现了问题。您需要将postCollection声明为AngularFirestoreCollection。这是我为下面的代码所做的操作:
updatedb: AngularFirestoreCollection<item>;
然后我可以使用它而不会出现地图错误。希望这会有所帮助!
过去(可行,但不是最好的)答案
我遇到了同样的问题。我认为这与您的postCollection变量有关。当我把完整的db.collection ...调用放到那里时,它对我有用。不确定是否声明该变量(我只是将我作为通用变量,未分配在文件顶部。
这不起作用:
export class DataService {
items$: Observable<any>;
selectedItem$: Observable<any>;
selected3x3: item = new item();
timeout;
email: string;
updatedb;
get3x3s(){
if(!this.email){
this.items$ = this.updatedb.snapshotChanges().pipe(
map(changes => {
return changes.map(doc => {
return{
id: doc.payload.doc.id,
data: doc.payload.doc.data()
}
})
})
);
但这确实可以编译:
export class DataService {
items$: Observable<any>;
selectedItem$: Observable<any>;
selected3x3: item = new item();
timeout;
email: string;
updatedb;
get3x3s(){
if(!this.email){
this.items$ = this.db.collection(this.email).snapshotChanges().pipe(
map(changes => {
return changes.map(doc => {
return{
id: doc.payload.doc.id,
data: doc.payload.doc.data()
}
})
})
);
答案 1 :(得分:0)
解决方案:
this.afStore.collection(`${uid}/ingresos-egresos/items`)
.snapshotChanges()
.pipe(
map(docData => {
return docData.map( doc => {
let data = doc.payload.doc.data() as IngresoEgreso; //here solutions
return {
uid: doc.payload.doc.id,
...data
};
});
})
)
.subscribe(docData => {
console.log(docData);
});