获取Firestore文档作为普通的Javascript对象?

问题描述 投票:1回答:2

我目前正在手动迭代firestore中的文档字段并将它们放入一个我将其字符串化为JSON的对象中。

有没有办法自动化这个过程?就像是:

var userEnrollments = ToJson(await admin.firestore().collection(USERS + "/" + x.uid + "/" + ENROLMENT));
javascript json firebase google-cloud-firestore
2个回答
2
投票

DocumentSnapshot有一个方法data(),它将文档的全部内容(没有子集合)作为纯JavaScript对象返回。

admin.firestore().doc('path/to/doc').get().then(snapshot => {
    const data = snapshot.data()  // a plain JS object 
})

0
投票

尝试使用observable

var userEnrollments = Observable<User>;
Document userDoc = this.db.doc<User>('User/'+id);
this.userEnrollments = this.userDoc.valueChanges();

你可以使用像Observable<User[]>;这样的数组:FirestoreCollection<User>;

我在角火中使用类似的东西。你也可以在firebase中使用ASYNC

© www.soinside.com 2019 - 2024. All rights reserved.