Firebase cloud function, when i am trying to fetch data and assigning to array, it says
TypeError: snapshot.forEach is not a function
Could you please help me to sort this out. I don't know what's wrong with this.
let userTokenList = [];
db.collection("UserTokens").doc(UserId).get().then(snapshot => {
snapshot.forEach(doc => {
var newelement = {
"Token": doc.data().Token,
}
console.log(newelement);
userTokenList = userTokenList.concat(newelement);
});
});
CodePudding user response:
You're getting a single document, so the result in your snapshot
variable is a DocumentSnapshot
and that doesn't have a forEach
method.
If you want to get a single document, ditch the forEach
loop and do:
db.collection("UserTokens").doc(UserId).get().then(doc => {
var newelement = {
"Token": doc.data().Token,
}
console.log(newelement);
userTokenList = userTokenList.concat(newelement);
});
If you want to get all documents in the collection, do:
db.collection("UserTokens").get().then(snapshot => {
snapshot.forEach(doc => {
var newelement = {
"Token": doc.data().Token,
}
console.log(newelement);
userTokenList = userTokenList.concat(newelement);
});
});