I'm currently having trouble with some cloud functions that only writes after another instance is triggered.
My cloud function code is below and is triggered from a pubsub event.
if (jsonData.Event == "TEST") {
var msg = jsonData.Value;
var timeOfReception = new Date();
//changer l'occupation courante
docRef
.get()
.then(function (doc) {
if (doc.exists) {
docRef.update({
LastGesture: msg,
}).then(function() {
console.log("Write completed")
}).catch(function(error) {
console.error("Write failed: "+error)
});
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
})
.catch(function (error) {
console.log("Error getting document:", error);
});
}
Here is the log from the same cloud function :
2021-07-02 15:44:17.869 EDTpubsubToFireStoregsn141ejhe1b Function execution started
Default
2021-07-02 15:44:17.874 EDTpubsubToFireStoregsn141ejhe1b {Date: 2000/00/00-00:00:00, DeviceID: TSA00001, Event: TEST, Location: MAISON, Value: PRESENT, Version: 1.0, tableName: GestureTest}
Debug
2021-07-02 15:44:17.875 EDTpubsubToFireStoregsn141ejhe1b Function execution took 7 ms, finished with status: 'ok'
Default
2021-07-02 15:44:18.532 EDTpubsubToFireStoregsn141ejhe1b Write completed
Debug
2021-07-02 16:04:27.466 EDTpubsubToFireStore2o6osfooylkj Function execution started
Default
2021-07-02 16:04:27.513 EDTpubsubToFireStore2o6osfooylkj {Date: 2000/00/00-00:00:00, DeviceID: TSA00001, Event: TEST, Location: MAISON, Value: ABSENT, Version: 1.0, tableName: GestureTest}
Debug
2021-07-02 16:04:27.548 EDTpubsubToFireStore2o6osfooylkj Function execution took 85 ms, finished with status: 'ok'
Debug
2021-07-02 16:05:40.824 EDTpubsubToFireStore2o6ozvzxeri1 Function execution started
Default
2021-07-02 16:05:41.347 EDTpubsubToFireStore2o6ozvzxeri1 {Date: 2000/00/00-00:00:00, DeviceID: TSA00001, Event: TEST, Location: MAISON, Value: PRESENT, Version: 1.0, tableName: GestureTest}
Debug
2021-07-02 16:05:41.351 EDTpubsubToFireStore2o6ozvzxeri1 Function execution took 528 ms, finished with status: 'ok'
Default
2021-07-02 16:06:07.130 EDTpubsubToFireStore2o6ozvzxeri1 Write completed
Default
2021-07-02 16:06:07.830 EDTpubsubToFireStore2o6ozvzxeri1 Write completed
As you can see, the function at 15:44:17 works perfectly: The function is triggered and the write operation is performed. However, the function at 16:04:27 doesn't write until the function at 16:05:41 is triggered which as it shows, does the two writes almost at the same time.
I'm not really sure how to explain this behaviour as I'm still new to cloud functions. If there is anything that could point me to the right direction I would be really grateful :). I can provide further details if needs be. Thank you!