简体   繁体   English

使用 AngularFireStore 将文档/集合返回为 object

[英]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.tsdocument.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'))
}

More info on docs 有关文档的更多信息

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.docthis.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.

相关问题 带有子查询的AngularFireStore返回集合 - AngularFireStore return collection with subquery AngularFirestore:检索集合中的文档引用并将 map 检索到 class - AngularFirestore: retrieving a document reference in a collection and map it to a class 使用AngularFirestore Collection orderBy和snapShotChanges方法 - Using AngularFirestore Collection orderBy with snapShotChanges method 使用AngularFirestore和firebase的“ ERROR TypeError:Object(…)不是函数” - “ERROR TypeError: Object(…) is not a function” using AngularFirestore and firebase 在使用“where()”时,在AngularFirestore中查询集合时不会产生任何数据 - Querying Collection in AngularFirestore results in no data when using “where()” 无法在Material Datatable中使用AngularFirestore获取对象键 - Can't Fetch Object Keys Using AngularFirestore in Material Datatable 基于angularfirestore集合中的子集合的字段值进行搜索 - Search based on a field value of subcollection in angularfirestore collection 通过快照更改将HTML与AngularFirestore集合绑定 - Bind HTML with AngularFirestore collection via snapshotChanges 如何从另一个文档angularId的Id获取文档 - How to get a document from Id gotten from another document angularFirestore 结合使用ionic 3的AngularFireStore(Angular 5 + rxjs5) - Using AngularFireStore with ionic 3 (angular 5 + rxjs5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM