I have a cloud function that runs with no errors, however, it won't write to the database after Promise.all
I've tested this function in a web browser and the data seems to resolve correctly after the Promise.all. When I put this into a cloud function a console.log() after Promise.all shows the original data and not the updated data, as if it's skipping the forEach and resolving right away. I don't get this behavior when the function runs in a browser.
On top of that, the data will not write to Firestore if it's after the Promise.all any data write before that goes through just fine.
exports.scheduledIndexUpdate = functions.pubsub
.schedule("every 30 minutes")
.onRun(context => {
console.log("This will be run every 30 minutes!");
var newIndex;
var getIndex = new Promise((resolve, reject) => {
admin
.firestore()
.collection("billsIndex")
.doc("GS2019Index")
.get()
.then(doc => {
if (doc.exists) {
newIndex = doc.data();
resolve(newIndex);
}
});
});
return getIndex.then(index => {
var keys = Object.keys(index);
keys.forEach(function(key) {
admin
.firestore()
.collection("billsMetaData")
.doc(key)
.get()
.then(doc => {
if (doc.exists) {
const metaData = doc.data();
let agree = 0,
disagree = 0,
neutral = 0,
somewhatAgree = 0,
somewhatDisagree = 0;
if (metaData.votes) {
if (metaData.votes.agree) {
agree = metaData.votes.agree;
}
if (metaData.votes.disagree) {
disagree = metaData.votes.disagree;
}
if (metaData.votes.neutral) {
neutral = metaData.votes.neutral;
}
if (metaData.votes.somewhatAgree) {
somewhatAgree = metaData.votes.somewhatAgree;
}
if (metaData.votes.somewhatDisagree) {
somewhatDisagree = metaData.votes.somewhatDisagree;
}
newIndex[key].userVotes =
agree + disagree + neutral + somewhatAgree + somewhatDisagree;
}
}
});
});
Promise.all(keys).then(function(result) {
admin
.firestore()
.collection("billsIndex")
.doc("GS2019Index2")
.set({
newIndex
});
console.log(newIndex);
console.log("done");
});
});
});
Any help would be appriciated!