[英]Return document/collection as object using AngularFireStore
I'm editing the question to make it more clear:我正在编辑问题以使其更清楚:
Using this code will return a Promise, very useful in some case.使用此代码将返回 Promise,在某些情况下非常有用。 Service.ts:
服务.ts:
async getElement(id: string): Promise<AngularFirestoreDocument<Element>> {
return this.firestore.doc<Element>(`/element/${id}`);
}
I'm looking for a way to return it as an object so it not linked with firebase anymore.我正在寻找一种方法将其作为 object 返回,因此它不再与 firebase 链接。
Here is my page.ts这是我的 page.ts
async ngOnInit() {
this.elementId = this.route.snapshot.paramMap.get('id');
this.elementService.getElement(this.elementId).then(element$ => {
this.element= element$.valueChanges();
});
}
Any ideas on how to do it?关于如何做的任何想法?
I want to be able to do (off-line) operations like我希望能够进行(离线)操作,例如
this.element.name = 'New name';
and和
{{element.name}}
I also have the same question for collections.对于 collections,我也有同样的问题。
It looks like you are using AngularFire看起来您正在使用AngularFire
On document.service.ts
在
document.service.ts
上
constructor(private firestore: AngularFirestore) { }
addDocument(document: DocumentInterface): Observable<any> {
this.documentCollection = this.firestore.collection(`documents/`);
return from(this.documentCollection.add(document));
}
To use it just inject documentService
service.要使用它,只需注入
documentService
服务。
constructor(private documentService: DocumentService) {
}
createDocument(document: DocumentInterface) {
this.documentService.addDocument(document).subscribe(() => console.log('Success'), () => console.log('error'))
}
If I understood you correctly you want to get a document, break the connection with the firebase and use it as a simple javascript object.如果我理解正确,您想要获取文档,请断开与 firebase 的连接并将其用作简单的 javascript object。 In this case your document fetching flow should look as follows:
在这种情况下,您的文档获取流程应如下所示:
this.firestore.doc('path/to/doc').get().toPromise().then(res => {
this.doc = res.data();
});
For collections the implementation is pretty similar对于 collections,实现非常相似
this.firestore.collection('/path/to/collection').get().toPromise().then(res => {
this.collection = res.docs.map(d => d.data());
});
In this case, you're getting a simple javascript object as this.doc
and array in this.collection
which you can use as you want.在这种情况下,您将获得一个简单的 javascript object 作为
this.doc
和this.collection
中的数组,您可以随意使用。 However, none of your changes will affect firebase entities in the cloud directly.但是,您的任何更改都不会直接影响云中的 firebase 实体。 You'll need to create separate methods for this.
您需要为此创建单独的方法。
Please, let me know if I understood you correctly and my answer helps or you have some questions remaining.请让我知道我是否正确理解您并且我的回答有帮助,或者您还有一些问题。
valueChanges
returns and observable so you need to render it using the async
pipe valueChanges
返回且可观察,因此您需要使用async
pipe 呈现它
{{ (element | async)?.name }}
If you want to update it locally, you need to subscribe如果要在本地更新,需要订阅
this.firestore.doc(`/element/${id}`).valueChanges()
.pipe(
take(1)
)
.subscribe(doc => {
this.element = doc;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.