Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
like image 737
meds Avatar asked Mar 08 '23 05:03

meds


1 Answers

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 
})
like image 141
Doug Stevenson Avatar answered Mar 10 '23 11:03

Doug Stevenson