1

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?

1 Answer 1

1

I don't think your exports are automatically available to the local file. So this might work better:

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;
    });
 };
exports.handleOrderEvent = handleOrderEvent;

So this first defined the handleOrderEvent function, that you call from the processEvent export. It then also exports handleOrderEvent, so other scripts (or Cloud Functions clients) can call it. If you don't need the latter, you can simply remove that last line.

Sign up to request clarification or add additional context in comments.

1 Comment

Frank - thank you! I implemented as suggested, without the last line, and it is working as expected now. I did have to deploy it twice - perhaps just too impatient on my part. Very much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.