如何在顶层正确初始化具有参数化配置的自定义类实例?

huangapple go评论74阅读模式
英文:

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

问题

I will only provide translations for the code part you mentioned:

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(),
);

Please note that the warnings you've received in your edited message are advising against using .value() during deployment and suggest using Params directly in your configuration.

英文:

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 >

答案1

得分: 1

这些是您的错误消息。请仔细阅读:

在函数部署期间调用 params.TASKS_QUEUE_NAME.value(),而不是在运行时。

这通常是一个错误。在配置中,直接使用 Params 而不要调用 .value()。

例如:{ memory: memoryParam } 而不是 { memory: memoryParam.value() }

这些消息告诉您,只有在函数代码运行时才应该使用参数值,而不是在部署期间。这意味着您应该将 cloudTasksUtil 的定义移动到函数内部,而不是在全局范围内。

请遵循您在文档中看到的示例。请注意,参数在全局范围内定义,它们的值仅在函数主体内部使用。

// 定义一些参数
const minInstancesConfig = defineInt('HELLO_WORLD_MININSTANCES');
const welcomeMessage = defineString('WELCOME_MESSAGE');

// 要在函数的配置中使用配置的参数,请直接提供它们。
// 要在运行时使用它们,请在它们上调用 .value()。
export const helloWorld = onRequest(
  { minInstances: minInstancesConfig },
  (req, res) => {
    res.send(`${welcomeMessage.value()}!我是一个函数。`);
  }
);
英文:

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.`);
  }
);

huangapple
  • 本文由 发表于 2023年5月24日 20:30:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定