【问题标题】:Firebase cloud function server side global variablesFirebase 云函数服务器端全局变量
【发布时间】:2018-02-20 08:53:03
【问题描述】:

Firebase 云函数上是否可以有一种全局变量?

我的意思是我可以有一个类似的 index.js,其中设置一个全局变量,比如说panicModeVariable。

而且我想在做任何事情之前检查我的云函数这个变量,比如这里的 auth create user trigger。

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
    }
    [...]

我尝试了两个虚拟函数

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

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

但我似乎无法按预期方式访问此变量。

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

我想这样做,如果可能的话,有一种服务器端变量可以直接读取,而不需要调用 SDK 来读取,比如说,数据库存储的值 (这样就没有函数调用计数)。

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 2021-11-17
      相关资源
      最近更新 更多