angular - AngularFire Firestore 属性“地图”在“操作”类型上不存在>'
问题描述
我正在关注有关集合的firebase 文档,但在尝试检索具有文档 ID 的文档集合时,无法弄清楚为什么我的代码无法按照文档工作。
我的 IDE 显示以下错误:
'Action< DocumentSnapshot< any>>' 类型上不存在属性 'map'。
对于以下代码:
this.userDocRef = this.afs.doc<any>(`users/${user.uid}`);
this.userDoc$ = this.userDocRef.snapshotChanges().pipe(
map(actions => {
return actions.map(a => { // error throws here
const data = a.payload.doc.data() as any;
const id = a.payload.doc.id;
return { id, ...data };
})
})
)
当我运行此代码时,我收到以下错误:
错误类型错误:actions.map 不是函数
如何重构它以使其正常工作?
"firebase": "^5.0.4"
"@angular/core": "^6.1.0"
"angularfire2": "^5.0.0-rc.10"
解决方案
您的映射代码错误:
map(actions => {
return actions.map(a => { // error throws here
const data = a.payload.doc.data() as any;
const id = a.payload.doc.id;
return { id, ...data };
})
})
这用于映射集合。您不是在迭代一个集合,而是一个文档
尝试:
this.userDoc$ = this.userDocRef.snapshotChanges()
.pipe(
map(action => {
const data = action.payload.data();
const id = action.payload.id;
return { id, ...data };
})
);
推荐阅读
- r - 如何遍历多个数据集从r中的指定列中删除特定字符
- java - 将 WAR 添加到 spring-boot-web Jetty 实例
- html - 在不适合对象的情况下保持比例图像按比例放大或缩小
- karaf - Karaf 功能:安装抛出不受支持的“Bundle-ManifestVersion”值:1
- python - 将掩码按其元素划分为其他掩码
- php - fpdf 在字符串中插入新行
- python - 平面图文本识别和 OCR
- python - 使用 get_url() 或 url() 访问端点的正确方法是什么?
- java - 标签的宽度属性何时更新?
- php - 如何以编程方式更改 Wordpress 的默认帖子永久链接?