如何从快照中获取有效载荷和文档 ID 角度 6 中的更改?



>我有以下代码片段,在 angular 6 中不起作用.编译器显示错误为内部映射函数的"属性'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"
},

谢谢。

使用真实答案进行编辑

我发现了问题。您需要将 postCollection 声明为 AngularFirestoreCollection。以下是我为下面的代码执行此操作的方法:

updatedb: AngularFirestoreCollection<item>;

然后我可以使用它而不会收到地图错误。希望这有帮助!

过去(有效,但不是最好的(答案

我一直遇到同样的问题。我认为这与你的 postCollection 变量有关。当我把完整的数据库集合...打电话给那里,它对我有用。不确定这是否是关于如何声明该变量(我只是将我的变量作为通用变量,未在文件顶部分配。

这不起作用:

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()
}
})
})
);

解决方案:

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

最新更新