Search code examples
node.jstypescriptfirebasegoogle-cloud-functionsfirebase-tools

How to properly initialize custom class instance with parameterized configuration in top-level?


How can I use these defineString(), defineInt, ... in top-level custom class constructor?

defineString() returns StringParam which has value()method. I want to use parameterized configuration to initialize my custom class instance in top-level.

import {defineString, projectID} from "firebase-functions/params";

const cloudTasksUtil = new CloudTasksUtil(
    defineString("TASKS_QUEUE_NAME").value(),
    defineString("MY_TASKS_LOCATION").value(),
    defineString("G_SERVICE_ACCOUNT_EMAIL").value(),
    projectID.value(),
);

If I use .value() method as above, it keeps warning that is is mistake and I should use StringParam itself. I think I should use StringParam because the instance is initialized before the cloud functions.

I couldn't find any resources related this issue.

EDIT) When I run firebase functions:shell, it shows below.

✔  functions: Using node@18 from host.
i  functions: Loaded environment variables from .env, .env.local.
{"severity":"WARNING","message":"params.TASKS_QUEUE_NAME.value() invoked during function deployment, instead of during runtime."}
{"severity":"WARNING","message":"This is usually a mistake. In configs, use Params directly without calling .value()."}
{"severity":"WARNING","message":"example: { memory: memoryParam } not { memory: memoryParam.value() }"}
{"severity":"WARNING","message":"params.MY_TASKS_LOCATION.value() invoked during function deployment, instead of during runtime."}
{"severity":"WARNING","message":"This is usually a mistake. In configs, use Params directly without calling .value()."}
{"severity":"WARNING","message":"example: { memory: memoryParam } not { memory: memoryParam.value() }"}
{"severity":"WARNING","message":"params.G_SERVICE_ACCOUNT_EMAIL.value() invoked during function deployment, instead of during runtime."}
{"severity":"WARNING","message":"This is usually a mistake. In configs, use Params directly without calling .value()."}
{"severity":"WARNING","message":"example: { memory: memoryParam } not { memory: memoryParam.value() }"}
{"severity":"WARNING","message":"params.PROJECT_ID.value() invoked during function deployment, instead of during runtime."}
{"severity":"WARNING","message":"This is usually a mistake. In configs, use Params directly without calling .value()."}
{"severity":"WARNING","message":"example: { memory: memoryParam } not { memory: memoryParam.value() }"}
i  functions: Loaded functions: helloWorld, reserveSeat, startTimer, countSeatChange
i  functions: Connected to running firestore emulator at 127.0.0.1:8080, calls to this service will affect the emulator
i  functions: Connected to running database emulator at 127.0.0.1:9000, calls to this service will affect the emulator
⚠  functions: The following emulators are not running, calls to these services will affect production: pubsub, storage, eventarc
firebase >

Solution

  • These are your error messages. Read them carefully:

    params.TASKS_QUEUE_NAME.value() invoked during function deployment, instead of during runtime.

    This is usually a mistake. In configs, use Params directly without calling .value().

    example: { memory: memoryParam } not { memory: memoryParam.value() }

    These messages are telling you that you should only use parameter values when your function code is running, not during deployment. That means you should move your definition of cloudTasksUtil inside your function rather than at the global scope.

    Follow the example that you see in the documentation. Note that the parameters are defined at the global scope, and their values are only used within the function body.

    // Define some parameters
    const minInstancesConfig = defineInt('HELLO_WORLD_MININSTANCES');
    const welcomeMessage = defineString('WELCOME_MESSAGE');
    
    // To use configured parameters inside the config for a function, provide them
    // directly. To use them at runtime, call .value() on them.
    export const helloWorld = onRequest(
      { minInstances: minInstancesConfig },
    (req, res) => {
        res.send(`${welcomeMessage.value()}! I am a function.`);
      }
    );