I have a firebase function which runs on value changes on Realtime Database. Selectively some times it fails and sometime it executes properly. The code in question is as below
return waitForDelare(tableId).then(() => {
console.log(tableId + ": Declare Resolved");
return admin.database().ref(`table/${tableId}`).once("value").then( tab => {
console.log("Came Here");
if (!tab.exists()) {
console.log(tableId + ": Tab does not exist");
}
return Promise.all([
admin.firestore().collection(tab.val().basic.gameCategory).doc(tableId).set(tab.val()),
admin.database().ref(`table/${tableId}`).remove()
]).then(() => {
console.log(tableId + ": Success in Archive");
return true;
})
.catch(err => {
console.log(tableId + ": Error in Archive" + err);
return false;
});
}).catch(err => {
console.log(tableId + ": Error: " + err);
return false;
});
}).catch(() => {
console.log(tableId + ": Declare Rejected");
return admin.database().ref(`table/${tableId}`).once("value").then(tab => {
if (!tab.exists()) {
console.log(tableId + ": tab does not exist");
}
return Promise.all([
admin.firestore().collection(tab.val().basic.gameCategory).doc(tableId).set(tab.val()),
admin.database().ref(`table/${tableId}`).remove()
]).then(() => {
console.log(tableId + ": Success in Archive");
return true;
})
.catch(err => {
console.log(tableId + ": Error in Archive" + err);
return false;
});
}).catch(err => {
console.log(tableId + ": Error: " + err);
return false;
});
});
Here sometimes the code run perfectly but sometime it does not proceed after console.log(tableId + ": Declare Resolved");
this line.
The log of failed run is as below:
aEg5qxcaVNLILdqOGOpO: Declare Resolved
Unhandled rejection
Uncaught exception
TypeError: Cannot read property 'stack' of undefined at logAndSendError (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:174:23) at process.on.err (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:390:13) at process.emit (events.js:198:13) at process.EventEmitter.emit (domain.js:448:20) at emitPromiseRejectionWarnings (internal/process/promises.js:140:18) at process._tickCallback (internal/process/next_tick.js:69:34)
Error: Process exited with code 16 at process.on.code (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:393:29) at process.emit (events.js:198:13) at process.EventEmitter.emit (domain.js:448:20) at process.exit (internal/process/per_thread.js:168:15) at logAndSendError (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:184:9) at process.on.err (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:386:13) at process.emit (events.js:203:15) at process.EventEmitter.emit (domain.js:448:20) at process._fatalException (internal/bootstrap/node.js:497:27)