I am saving data to firebase successfully
JavaScript
x
20
20
1
const productsRef = db.collection("store").doc(userID).collection("products");
2
3
productsRef
4
.doc(productID)
5
.set({
6
userID: userID,
7
productCatagory: productCatagory.value,
8
productCode: productID,
9
productName: productName.value,
10
price: price.value,
11
description: description.value,
12
time: time,
13
})
14
.then(function () {
15
console.log("Document successfully written!");
16
})
17
.catch(function (error) {
18
console.error("Error writing document: ", error);
19
});
20
But when i try to retrieve it firestore sends “No such document”. Trying to get the array of objects.
JavaScript
1
15
15
1
db.collection("store")
2
.doc(userID)
3
.get()
4
.then((doc) => {
5
if (doc.exists) {
6
console.log(doc.data());
7
} else {
8
// doc.data() will be undefined in this case
9
console.log("No such document!");
10
}
11
})
12
.catch(function (error) {
13
console.log("Error getting document:", error);
14
});
15
Here is the db
Edit: I found out that in order to access all documents you have to do it this way.
JavaScript
1
13
13
1
db.collection("store")
2
.get()
3
.then((querySnapshot) => {
4
querySnapshot.forEach((doc) => {
5
// doc.data() is never undefined for query doc snapshots
6
7
console.log(doc.data());
8
});
9
})
10
.catch((e) => {
11
console.log(e);
12
});
13
It goes into the then block but then querySnapshot.forEach doesnt run
Advertisement
Answer
Okay so i found the solution.
JavaScript
1
12
12
1
db.collection(`store/${userID}/products`)
2
.get()
3
.then((querySnapshot) => {
4
querySnapshot.forEach((doc) => {
5
6
console.log(doc.data());
7
});
8
});
9
10
11
12