gpt4 book ai didi

AngularFire Firestore 属性 'map' 在类型 'Action>' 上不存在

转载 作者:行者123 更新时间:2023-12-03 22:06:50 25 4
gpt4 key购买 nike

我正在关注 firebase docs在集合上,无法弄清楚为什么我的代码在尝试检索具有文档 ID 的文档集合时无法按照文档工作。

我的 IDE 显示以下错误:

Property 'map' does not exist on type 'Action< DocumentSnapshot< any>>'.

对于下面的代码:

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

当我运行这段代码时,出现以下错误:

ERROR TypeError: actions.map is not a function

我如何重构它以使其正常工作?

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

关于AngularFire Firestore 属性 'map' 在类型 'Action<DocumentSnapshot<any>>' 上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51842530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com