meds
meds

Reputation: 22916

Get Firestore document as plain Javascript object?

I'm currently manually itearting over document fields in firestore and putting them into an object which I stringify to JSON.

Is there a way to automate the process? Something like:

var userEnrollments = ToJson(await admin.firestore().collection(USERS + "/" + x.uid + "/" + ENROLMENT));

Upvotes: 5

Views: 5565

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317372

DocumentSnapshot has a method data() that returns the entire contents (without subcollections) of the document as a plain JavaScript object.

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

Upvotes: 8

Diego Venâncio
Diego Venâncio

Reputation: 6007

Try use observable

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

You can use array like: Observable<User[]>; with FirestoreCollection<User>;

I use similar in angular fire. You can use ASYNC in firebase too.

Upvotes: 0

Related Questions