如何为Firebase的Cloud Functions设置本地环境变量 [英] How do you setup local environment variables for Cloud Functions for Firebase

查看:137
本文介绍了如何为Firebase的Cloud Functions设置本地环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用http云功能来侦听请求,然后返回一条简单消息.

I'm using http cloud functions to listen for a request and then return a simple message.

我正在使用以下方法在本地开发云功能:

I'm developing cloud functions locally using:

firebase serve --only functions

我已经使用设置了一些自定义环境变量

I've setup some custom environment variables using

firebase functions:config:set

在部署项目时,使用以下代码访问自定义配置变量即可正常工作

Accessing the custom config variables using the below code works fine when the project is deployed

 functions.config()

,但是在本地开发时不起作用.通过单击以下命令触发功能时: http://localhost:5002/my-project -name/us-central1/functionName 我无法访问自定义配置变量.在本地使用functions.config()时,我可以看到默认配置,但看不到我的自定义配置变量

but it does not work when developing locally. When the function is triggered by hitting: http://localhost:5002/my-project-name/us-central1/functionName I can't access the custom config variables. when using functions.config() locally, I can see the default config, just not my custom config variables

在本地工作时,环境变量是否有替代解决方案或最佳实践?

Is there an alternate solution or best practice for environment variables when working locally?

推荐答案

到目前为止,您必须通过运行此命令在功能目录内内手动创建.runtimeconfig.json文件.然后运行serve命令.

As of now, you have to manually create a .runtimeconfig.json file inside your functions directory by running this command. Then run the serve command.

firebase functions:config:get > .runtimeconfig.json

如果您使用的是Windows Powershell,请将以上内容替换为:

If you are using Windows Powershell, replace the above with:

firebase functions:config:get | ac .runtimeconfig.json

您可以在 https://firebase.google.com/docs/functions中了解更多信息/local-emulator

这篇关于如何为Firebase的Cloud Functions设置本地环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆