Firebase云功能服务器端全局变量 [英] Firebase cloud function server side global variables
问题描述
在Firebase云功能上可能有某种全局变量吗?
It's possible to have a sort of a global variable on firebase cloud functions?
我的意思是我可以拥有一个 index.js ,其中可以设置一个全局变量,例如panicModeVariable
.
I mean I could have an index.js like that in which set-up a global variable, let's say panicModeVariable
.
我想在做任何事情之前先在云函数中检入此变量,例如auth create用户触发器中的此处.
And I would like to check in my cloud functions this variable before doing anything, like here in the 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);
});
但是似乎我无法按照预期的方式访问此变量.
But it seems that I cannot access this variable in the way I intended.
写函数使用局部"变量,而读函数始终使用初始值.
The writing function it uses a 'local' variable, while the reading one uses the initial value, always.
如果可能的话,我想这样做是让某种服务器端变量直接读取而无需调用SDK来读取数据库存储值(这样就不会进行函数调用计数).
I'd like to do that, if it is possible, to have a sort of server side variable to be read directly without the need to a call to the SDK to read, let's say, a database stored value (so that to not have a function call counting).
推荐答案
You could use Environment Config variables https://firebase.google.com/docs/functions/config-env
据我所知,您无法在函数本身中进行设置,需要在上载函数之前通过CLI进行设置.
As far as I'm aware, you can't set them in the function themselves, they need to be set by CLI before you upload a function.
您可以执行类似firebase functions:config:set panic.mode=true
然后在您的createUserAccount
函数中,您可以调用functions.config().panic.mode
Then in your createUserAccount
function you could call functions.config().panic.mode
但这不能通过https
触发器帮助您set
变量.为此,您需要使用数据库.
But this won't help you set
the variable via the https
trigger. For that you'll need to make use of the database.
这篇关于Firebase云功能服务器端全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!