I have an app which I've been running successfully on a firebase live environment, but when I try to run it on a local emulator set-up, it fails.
firebase emulators:start --only functions,database, with the following firebase.json config:{
"functions": {
"source": "functions"
},
"emulators": {
"functions": {
"port": 5001
},
"ui": {
"enabled": true
},
"database": {
"port": 9000
}
},
"database": {
"rules": "database.rules.json"
}
}
2: open the emulator UI, switch to the "Realtime Database" tab, and in the schema, create a "congregations" child, and then add some child data to that such as:
{
"congregationName": "My Congregation",
"email": "[email protected]",
"newEventData": {"capacity":123,"eventName":"My Event"}
}
The script (which I'll paste below) should then take the data and do some further-processing, editing and writing some additional data. This works fine on the live set-up, but on the local emulator, nothing happens and instead, I see an error appear in my firebase terminal.
I've cut the chaff and simplified the function to just the erroneous code:
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()) will output the example data that I entered in step 2, as expected. console.log(snap.ref) won't output anything to the terminal, except throw the following error, with a stack trace that points to the line where snap.ref is called:! 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: '[email protected]',
> 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.
I'm at a loss as to why this is happening, especially considering that I only get the problem on the local emulator. Any help would be much appreciated. For what it's worth, the URL which shows at the top of the Realtime Database tab is http://localhost:9000/ which makes sense, I'm not sure how that relates to the expected x.firebaseio.com url.
Looks like you found a bug in the firebase-functions sdk. I was experiencing the same thing and came across your post, as well as an open issue in their repository.
https://github.com/firebase/firebase-functions/issues/726
Sounds like the source of the bug has been identified and they're working on a fix.
from samtstern at firebase:
We are working on a fix. The issue is with change.after.ref, and the workaround is to build your own ref to the document inside the function using admin.database().ref()...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With