代码之家  ›  专栏  ›  技术社区  ›  Cod3Flu3nc3

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

  •  4
  • Cod3Flu3nc3  · 技术社区  · 8 年前

    有可能有一种 全局变量 关于firebase云功能?

    我是说我可以 指数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
        }
        [...]
    

    我尝试了两个虚拟函数

    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来读取数据库存储值 (这样就不会有函数调用计数)。

    1 回复  |  直到 8 年前
        1
  •  2
  •   sketchthat    8 年前

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

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

    你可以这样做 firebase functions:config:set panic.mode=true

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

    但这对你没有帮助 set 变量通过 https 触发为此,您需要使用数据库。

    推荐文章