小编Cod*_*nc3的帖子

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

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)

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

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

firebase google-cloud-functions

5
推荐指数
1
解决办法
5524
查看次数

标签 统计

firebase ×1

google-cloud-functions ×1