gpt4 book ai didi

javascript - Angular Firestore Valuechanges 获取文档引用

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

我正在使用 Angular 和 google cloud firestore 来加载数据。
我也有一个模型类,我们称之为 IMyModel

export interface IMyModel{
data: any;
id: string;
}
其中 id是firestore上文档的 id。我可以轻松加载它 var docs = this.firestore.collection('myCollection').valueChanges({ idField: 'id' }) as Observable<IMyModel[]>; 这很好用。
但是现在,我也希望此功能与文档引用一起使用。假设我改变了模型
export interface IMyModel{
data: any;
documentReference: DocumentReference;
}
我现在如何插入 documentReference 字段?我已经试过了 var docs = this.firestore.collection('myCollection').valueChanges({ ref: 'documentReference', }) as Observable<IMyModel[]>;但这不会插入字段。

最佳答案

valueChanges() 给你一个可观察的。您可以使用 .pipe(map(items => items.map(item => yourFunction(item)))) 将数据转换为您喜欢的:

interface IMyModel {
data: any;
ref: DocumentReference;
}
const myCollection = this.firestore.collection<{ data: any }>('myCollection');
const itemsWithId$ = myCollection.valueChanges({ idField: 'id' });
const itemsWithRef$: Observable<IMyModel[]> = itemsWithId$.pipe(
map(itemsWithId => {
return itemsWithId.map(item => {
return {
data: item.data,
ref: myCollection.doc(item.id).ref,
};
});
}),
);

关于javascript - Angular Firestore Valuechanges 获取文档引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66339167/

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