I am using Firebase cloud function to create new document from another document.
Basically, i have a field in a document called reps {} that takes userId as Key and and int as value.
i want to check if the sum of the values in reps {} is greater than 100(example).
i have an onUpdate function that works perfectly but i need to add this feature. i tried this:
var count = 0;
admin.firestore()
.collection("posts")
.doc(userId)
.collection("userPosts")
.doc(postId).get().then(doc =>
{
doc['reps'].values.forEach(val =>
{
count += val;
});
});
console.log(count);
With this query i get the reps map, How can i calculate the sum of all value inside a map:
admin
.firestore()
.collection("posts")
.doc(userId)
.collection("userPosts")
.doc(postId).get().then(function (doc)
{
if (doc.exists)
{
console.log(doc.get("reps"));
}
});