The issue
I'm trying to retrieve the document reference from a query. My code returns undefined
. I can get the path by extracting various parts of documentSnapshot.ref
, but this isn't straightforward.
What I'd like to return is a reference which I can then later use to .update
the document, without having to specify the collection and use documentSnapshot.id
The documentation for the path
property is here
My code
const db = admin.firestore();
return db.collection('myCollection').get().then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
console.log(`documentReference.id = ${documentSnapshot.id}`);
console.log(`documentReference.path = ${documentSnapshot.path}`);
// console.log(`documentReference.ref = ${JSON.stringify(documentSnapshot.ref)}`);
});
});
Output
documentReference.id = Jez7R1GAHiR9nbjS3CQ6
documentReference.path = undefined
documentReference.id = skMmxxUIFXPyVa7Ic7Yp
documentReference.path = undefined