I just started playing around with Firebase/Javascript and happily was able to stand up functionality very quickly. I have found lots of online examples to get started, but haven't been able to find one that addresses the following scenario. The context is a mobile app that will be logging events via a single web-hook-like API using this construction:
exports.processEvent = functions.firestore.document('Events/{eventId}').onCreate(event => {
console.log('>>>>> Event triage triggered');
// grab the eventType and the payload to determine delegation path
const eventType = event.get("eventType");
const eventPayload = event.get("payload");
// delegate to the correct event handler
switch (eventType) {
case "ORDER":
console.log("=> Event:ORDER received: ", eventPayload);
handleOrderEvent(eventPayload);
break;
case "SHIP":
console.log("=> Event:SHIP received: ", eventPayload);
handleShipEvent(eventPayload);
break;
default:
console.error("=> Event:UNKNOWN received: ", eventPayload);
}
return true;
});
And I define the handler function as:
exports.handleOrderEvent = function handleOrderEvent(payload) {
const orderDB = firestore.collection("Orders");
// Add the new order to the database
orderDB.add(payload)
.then(function(docRef) {
console.log("Created new Order: ", docRef.id);
return;
})
.catch(function(error) {
console.error("Error adding Order:", error);
return;
});
};
But, at runtime I get the following error:
ReferenceError: handleOrderEvent is not defined
I just don't want to have a gigantic switch() statement inside the processEvent() function, so was trying to decompose it into more manageable chunks.
What am I missing in the declaration of the handler function that prevents the processEvent() function from being able to find it? I have tried placing it above the processEvent() function in the index.js file. I have just declared the function without attaching it to exports. I am sure it is something basic?