Firebase云函数服务器端全局变量

Cod*_*nc3 5 firebase google-cloud-functions

firebase 云函数上可以有某种全局变量吗?

我的意思是我可以有一个像这样的index.js,其中设置一个全局变量,比方说panicModeVariable

我想在执行任何操作之前检查我的云函数中的此变量,例如在 auth create user 触发器中。

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

var globalVariable = false;

// Create Account User
exports.createUserAccount = functions.auth.user().onCreate(event => {
    if (!globalVariable) {
        const uid = event.data.uid
        const email = event.data.email
        const photoUrl = event.data.photoURL
    }
    [...]
Run Code Online (Sandbox Code Playgroud)

我尝试使用两个虚拟函数

exports.setGlobal = functions.https.onRequest((request, response) => {
    globalVariable = true;
    response.send("Set " + globalVariable);
});

exports.getGlobal = functions.https.onRequest((request, response) => {
    response.send("Read " + globalVariable);
});
Run Code Online (Sandbox Code Playgroud)

但似乎我无法按照我想要的方式访问这个变量。

写入函数使用“局部”变量,而读取函数始终使用初始值。

如果可能的话,我希望能够直接读取某种服务器端变量,而无需调用 SDK 来读取数据库存储的值(这样就不会函数调用计数)。

ske*_*hat 2

您可以使用环境配置变量https://firebase.google.com/docs/functions/config-env

据我所知,您无法在函数本身中设置它们,需要在上传函数之前通过 CLI 设置它们。

你可以做类似的事情firebase functions:config:set panic.mode=true

然后在你的createUserAccount函数中你可以调用functions.config().panic.mode

但这不会帮助您set通过触发器获取变量https。为此,您需要使用数据库。