Firestore Cloud Function Trying To Use A Batch To Group Writes And Missing Return Promise
I am triggering this function in my cloud code. Before I added the returns and the batch lines, everything worked but it was getting errors for missing return promises and the writ
Solution 1:
Okay i see your problem here and correct me if I'm wrong, your current format is like this:
exports.updateFeeds = functions.firestore
.document('feedItems/{feedID}')
.onUpdate((change, context) => {
return somePromise.get().then(returnData=>{
dosomethingwith(returnData);
return anotherpromise.get().then(anotherreturndata=>{
dosomethingwith(anotherreturndata);
})
})
});
This style of promise writing will fail because you shouldn't nest your promises like that.
It should look something like this:
exports.updateFeeds = functions.firestore
.document('feedItems/{feedID}')
.onUpdate((change, context) => {
return somePromise.get()//initial return function its return value is passed to returnData
.then(returnData=>{//now we have the value that was returned from the function abovereturndosomethingwith(returnData);//do something with the data from somePromise.get()
}).then(somethingWasDoneWithReturnData=>{ //this is the result of the dosomethingwith function because we returned itreturn anotherPromiseFunction.get();// now we need to return another promise
}).then(returnFromAnotherPromiseFunction=>{//the result from anotherPromiseFunction gets passed into the block below this doSomeThingWith(returnFromAnotherPromiseFunction);doSomeThingWith(returnFromAnotherPromiseFunction);
})
});
Here is some of my code from one of my firebase functions that reverse geocodes addresses.
return orderRef.get().then(doc=>{
let order = doc.data();
return order;
}).then(order=>getAddresses(order.rectangles))
.then(allAddresses=>eliminateDuplicates(allAddresses))
.then(duplicateAddressesRemoved=>batchReverseGeocode(duplicateAddressesRemoved))
.then(reverseGeocodedAddresses=> {
allAddresses = reverseGeocodedAddresses;
newAddressCount = allAddresses.length;
newChargeTotal = calculatePrice(newAddressCount);
console.log(allAddresses);
console.log(newAddressCount);
returnwriteFirebase(addressStorageRef, {addresses: reverseGeocodedAddresses}, null)
})
.then(noneObject=>updateFirebase(chargeRef, {status: "Charge Completed", finalChargeAmount: newChargeTotal, finalAddressCount: newAddressCount}, null))
.then(noneObject=>updateFirebase(orderRef, {status: "Paid In Full", finalChargeAmount: newChargeTotal, finalAddressCount: newAddressCount}, null))
.then(noneObject=>{
//now we need to update the charge and move onreturn stripe.charges.capture(stripeChargeID, {amount: newChargeTotal})
});
Post a Comment for "Firestore Cloud Function Trying To Use A Batch To Group Writes And Missing Return Promise"