【发布时间】:2020-09-30 07:14:14
【问题描述】:
背景
我有一个已在 firebase 实时环境中成功运行的应用,但是当我尝试在本地模拟器设置上运行它时,它失败了。
步骤
- 运行
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"}
}
- 观察...
脚本(我将在下面粘贴)然后应该获取数据并进行一些进一步的处理、编辑和写入一些额外的数据。这在实时设置上运行良好,但在本地模拟器上,没有任何反应,相反,我看到我的 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