【发布时间】: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