Reputation: 99
I am looking to get a document by using the formId
field in firebase with Node.js . The formId
is in a map so I'm not sure how to use the get()
function for it. Here is my React code which hasn't worked. I believe it doesnt work because the formId
field is contained in the formMetaData
map.
useEffect(() => {
const firestoreRef = firebase.firestore().collection('Forms');
// Create a query against the collection where we can match the formId
const queryRef = firestoreRef.where('formId', '==', ID_HERE);
}, []);
Also here is the set up of the DB:
Thank you!
Upvotes: 0
Views: 2566
Reputation: 50840
Use the dot notation to access nested fields:
useEffect(() => {
const firestoreRef = firebase.firestore().collection('Forms');
// Create a query against the collection where we can match the formId
const queryRef = firestoreRef.where('formMetaData.FormId', '==', ID_HERE);
^^^
queryRef.get().then((querySnapshot) => {
// total matched documents
const matchedDocs = querySnapshot.size
if (matchedDocs) {
querySnapshot.docs.forEach(doc => {
console.log(doc.id, "=>", doc.data())
})
} else {
console.log("0 documents matched the query")
}
})
}, []);
queryRef
is a Query and has get()
method which returns a Promise containing QuerySnapshot.
The docs
property on QuerySnapshot is an array of QueryDocumentSnapshot
and you can use data()
method on each DocumentSnapshot to access it's data.
Upvotes: 1
Reputation: 720
You can access the formId
via the formMetaData
object by updating your code as follows:
useEffect(() => {
const firestoreRef = firebase.firestore().collection('Forms');
// Create a query against the collection where we can match the formId
const queryRef = firestoreRef.where('formMetaData.formId', '==', ID_HERE);
}, []);
Upvotes: 1