【问题标题】:Firebase emulator: datasnapshot.ref throws "Cannot parse Firebase url..."Firebase 模拟器:datasnapshot.ref 抛出“无法解析 Firebase url ...”
【发布时间】:2020-09-30 07:14:14
【问题描述】:

背景

我有一个已在 firebase 实时环境中成功运行的应用,但是当我尝试在本地模拟器设置上运行它时,它失败了。

步骤

  1. 运行 firebase emulators:start --only functions,database,使用以下 firebase.json 配置:
{
  "functions": {
    "source": "functions"
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "ui": {
      "enabled": true
    },
    "database": {
      "port": 9000
    }
  },
  "database": {
    "rules": "database.rules.json"
  }
}

2:打开模拟器用户界面,切换到“实时数据库”选项卡,在架构中,创建一个“congregations”子节点,然后向其中添加一些子数据,例如:

{
  "congregationName": "My Congregation",
  "email": "test@test.com",
  "newEventData": {"capacity":123,"eventName":"My Event"}
}
  1. 观察...

脚本(我将在下面粘贴)然后应该获取数据并进行一些进一步的处理、编辑和写入一些额外的数据。这在实时设置上运行良好,但在本地模拟器上,没有任何反应,相反,我看到我的 firebase 终端出现错误。

代码+错误

我已经去除了糠秕并将函数简化为错误的代码:

exports.addCongregation = functions.database
  .ref("/congregations/{congregationId}")
  .onCreate(async (snap, context) => {    
    console.log(snap.val()); // runs fine, displays the entered data
    console.low(snap.ref); // throws an error without logging anything else
  });
  • console.log(snap.val()) 将按预期输出我在步骤 2 中输入的示例数据。
  • console.log(snap.ref) 不会向终端输出任何内容,除了抛出以下错误,堆栈跟踪指向调用 snap.ref 的行:
!  functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to GAC-staging.key.json. Non-emulated services will access production using these credentials. Be careful!
i  functions: Beginning execution of "addCongregation"
>  { congregationName: 'My Congregation',
>    email: 'test@test.com',
>    newEventData: { capacity: 123, eventName: 'My Event' } }
>  [2020-06-10T18:38:45.980Z]  @firebase/database: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com
!  functions: Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com
    at fatal (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\@firebase\database\dist\index.node.cjs.js:341:11)
    at parseRepoInfo (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\@firebase\database\dist\index.node.cjs.js:1295:9)
    at RepoManager.databaseFromApp (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\@firebase\database\dist\index.node.cjs.js:14986:25)
    at Object.initStandalone (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\@firebase\database\dist\index.node.cjs.js:15377:45)
    at DatabaseService.getDatabase (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\firebase-admin\lib\database\database.js:66:23)
    at FirebaseApp.database (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\firebase-admin\lib\firebase-app.js:231:24)
    at DataSnapshot.get ref [as ref] (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\firebase-functions\lib\providers\database.js:279:34)
    at exports.addCongregation.functions.database.ref.onCreate (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\index.js:86:22)
    at cloudFunction (C:\Users\Me\Projects\Web-Dev\prayin.io\functions\node_modules\firebase-functions\lib\cloud-functions.js:132:23)
    at runFunction (c:\Users\Me\AppData\Local\Yarn\Data\global\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:553:20)
!  Your function was killed because it raised an unhandled error.

我不知道为什么会发生这种情况,特别是考虑到我只在本地模拟器上遇到问题。任何帮助将非常感激。对于它的价值,显示在实时数据库选项卡顶部的 URL 是 http://localhost:9000/,这是有道理的,我不确定它与预期的 x.firebaseio.com url 有什么关系。

【问题讨论】:

  • 你的initializeApp() 方法看起来像firebase.google.com/docs/reference/js/firebase#initializeapp 吗?你检查过你的databaseURL 属性吗?你看到了什么?
  • 我以不同的方式尝试了initializeApp,但没有成功。在某些示例中,我将其留空,我还尝试对提供的配置进行硬编码,并尝试使用 GOOGLE_APPLICATION_CREDENTIALS 配置。无论我尝试什么,我都会遇到同样的错误。我还尝试在 localhost 和 firebase.io 地址之间切换 databaseURL
  • 控制台日志语句可能会抛出错误。你有一个错字——console.low

标签: firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

您似乎在 firebase-functions sdk 中发现了一个错误。我遇到了同样的事情,并看到了您的帖子,以及他们存储库中的一个未解决问题。

https://github.com/firebase/firebase-functions/issues/726

听起来错误的来源已经确定,他们正在努力修复。

来自 samtstern 在 firebase:

我们正在努力修复。问题出在 change.after.ref 上,解决方法是使用 admin.database().ref() 为函数内部的文档构建自己的 ref...

【讨论】:

  • 好地方。当我第一次遇到这个问题时,这张票不存在。一旦我知道这解决了问题,我将尽快测试这个理论并将其标记为已接受的答案。
  • 是的,它快把我逼疯了......否则模拟器非常棒!
猜你喜欢
  • 2016-03-08
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多