Hey I am new to firebase cloud functions, and I want to order a collection, which is stored in firestore, by a field and then write in each document of the collection the 'position' (this should happen every 60 minutes). After reading the documentations and some suggestions from Stackoverflow I tried following approach.
exports.scheduledFunctionPosition = functions.pubsub.schedule('every 60 minutes').onRun((context) => {
return admin.firestore().collection("example")
.orderBy("number", "desc")
.get()
.then(querySnapshot => {
if (!querySnapshot.empty) {
for(let i =0;i<querySnapshot.size;i++){
var queryDocumentSnapshotTmp = querySnapshot.docs[i];
queryDocumentSnapshotTmp.ref.update({'position':i+1});
}
return null;
} else {
console.log("");
return null;
}
});
As my programming skills aren't too good, I tried it the easy way, but I think there is a problem with the asynchronous programming. This code works perfect for the documents with the highest numbers. But when there are too much documents, this function won't update the last documents. Is there a solution for this?