Javascript 角度Firestore值更改获取文档引用
我正在使用Angular和google cloud firestore加载数据。 我还有一个模型类,我们称之为Javascript 角度Firestore值更改获取文档引用,javascript,angular,firebase,google-cloud-firestore,angularfire2,Javascript,Angular,Firebase,Google Cloud Firestore,Angularfire2,我正在使用Angular和google cloud firestore加载数据。 我还有一个模型类,我们称之为IMyModel export interface IMyModel{ data: any; id: string; } 其中id是firestore上文档的id。我可以很容易地将其加载 var docs=this.firestore.collection('myCollection').valueChanges({idField:'id'})作为可观察值这很好地工作
IMyModel
export interface IMyModel{
data: any;
id: string;
}
其中id
是firestore上文档的id
。我可以很容易地将其加载
var docs=this.firestore.collection('myCollection').valueChanges({idField:'id'})作为可观察值代码>这很好地工作
但是现在,我还需要这个带有文档引用的功能。假设我改变了模型
export interface IMyModel{
data: any;
documentReference: DocumentReference;
}
现在如何插入documentReference
字段?我已经试过了
var docs=this.firestore.collection('myCollection').valueChanges({ref:'documentReference',})作为可观察值代码>
但这不会插入字段。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,
};
});
}),
);
接口IMyModel{
资料:有;
参考:文件参考;
}
const myCollection=this.firestore.collection('myCollection');
const itemsWithId$=myCollection.valueChanges({idField:'id'});
const itemsWithRef$:Observable=itemsWithId$.pipe(
映射(itemsWithId=>{
返回itemsWithId.map(item=>{
返回{
数据:item.data,
参考:myCollection.doc(item.id).ref,
};
});
}),
);
您能试试这个解决方案吗
export interface Item { id: string; name: string; }
@Component({
selector: 'app-root',
template: `
<ul>
<li *ngFor="let item of items | async">
{{ item.name }}
</li>
</ul>
`
})
export class AppComponent {
private itemsCollection: AngularFirestoreCollection<Item>;
items: Observable<Item[]>;
constructor(private readonly afs: AngularFirestore) {
this.itemsCollection = afs.collection<Item>('items');
// .valueChanges() is simple. It just returns the
// JSON data without metadata. If you need the
// doc.id() in the value you must persist it your self
// or use .snapshotChanges() instead. See the addItem()
// method below for how to persist the id with
// valueChanges()
this.items = this.itemsCollection.valueChanges();
}
}
导出接口项{id:string;name:string;}
@组成部分({
选择器:'应用程序根',
模板:`
-
{{item.name}
`
})
导出类AppComponent{
私人物品收集:AngularFirestoreCollection;
项目:可观察;
构造函数(专用只读afs:AngularFirestore){
this.itemsCollection=afs.collection('items');
//.valueChanges()很简单。它只返回
//没有元数据的JSON数据。如果需要
//doc.id()中的值,您必须自己保存它
//或者改为使用.snapshotChanges()
//下面的方法介绍如何使用持久化id
//值更改()
this.items=this.itemsCollection.valueChanges();
}
}
有关更多参考信息,请参阅链接。当然您的函数
不需要是命名函数。可以用内联代码替换它(在大括号中,只需构建并返回所需的对象类型)。第一个map
是一个rxjs操作符(从“rxjs/operators”导入),它将每个数据段“映射”到新数据中。第二个map
是数组的“内置”方法,它将每个项“映射”到一个新项。@WouterVandenputte请参见代码