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!
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.
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);
}, []);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.