访问 Cloud Functions Firebase CLI 本地模拟器中的配置变量失败

Posted

技术标签:

【中文标题】访问 Cloud Functions Firebase CLI 本地模拟器中的配置变量失败【英文标题】:Access of configuration variable in Cloud Functions Firebase CLI local emulator failed 【发布时间】:2018-07-07 12:52:37 【问题描述】:

我正在使用 Firebase 函数。我通过命令设置了一个环境变量:

firebase functions:config:set my.token="abcde"

我通过命令验证了:

firebase functions:config:get

返回我:


  "my": 
    "token": "abcde"
  

在我的 index.js 中,我尝试通过以下方式获取设置的令牌:

const tok = functions.config().my.token

我想通过this document 在本地测试我的功能,所以我运行命令:

firebase serve --only functions

但它给了我错误:

Error from emulator. FirebaseError: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'token' of undefined

为什么我无法访问我在 Firebase 环境变量中设置的令牌?

【问题讨论】:

【参考方案1】:

这是 Firebase CLI 中的一个错误,已在 3.17.4 版中修复。请更新您的 firebase-tools 模块:

npm install -g firebase-tools

另外,在运行模拟器之前不要忘记按照文档中的说明将配置复制到本地文件:

cd functions
firebase functions:config:get > .runtimeconfig.json

【讨论】:

您是否记得按照您指向的文档中的建议创建.runtimeconfig.json 谢谢!这样可行!顺便说一句,你知道我每次更新配置设置时都需要运行命令复制到本地文件吗? 如果您想更改配置,是的,您必须使用这些更改重新创建文件以进行本地仿真。 也许这个答案已经过时了,但是the instructions 没有提到.runtimeconfig.json 文件。 很难搜索到这个.runtimeconfig.json 的东西,即使你知道你在找什么。终于又找到了这个答案。

以上是关于访问 Cloud Functions Firebase CLI 本地模拟器中的配置变量失败的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Cloud Functions 中访问云存储

在 Cloud Functions for Firebase 中访问 db 数据

Firebase Cloud Functions 访问 Firebase 托管文件

如何通过 Cloud Functions 将文件上传到 Cloud Storage 并使用 Firestore 控制对 Cloud Storage 的访问?

Cloud Functions for Firebase:无法访问 BigQuery

部署 Cloud Functions 后出现错误:无法刷新访问令牌。