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

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

问题描述

我正在使用 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

我已经使用

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 中了解更多信息/本地模拟器

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

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