Firebase-functions: Database ref from trigger throws error on firebase function emulator

Created on 2 Jul 2020  Â·  12Comments  Â·  Source: firebase/firebase-functions

Environment info

firebase-tools:
8.4.3

Platform:
Windows 10

Test case

import * as functions from 'firebase-functions'

exports.onDeviceOnlineChanged = functions.database.ref('/status/{deviceId}')
  .onWrite(async (change, context) => {
    const ref = change.before.ref.toString()
    // do something with the reference
    return ref
  })

Steps to reproduce

make a change to that node on the realtime database to initiate the function

Expected behavior

No error, this works on the server, but not on the emulator.

Calling the same code directly from the app runs without issues. The call to the change.before.ref causes the error only when the function is initiated from a trigger.

Calling using the initiated admin.database().ref('/status/${deviceId}') works normally as in the snippet below

exports.onDeviceChanged = functions.https.onCall(async (data, context) => {
  const deviceId = data.deviceId
  const status = admin.database().ref(`/status/${deviceId}`)

  const deviceFirestoreRef = admin.firestore().collection('devices').doc(deviceId)

  const upd = { online: status.toJSON() }
  return deviceFirestoreRef.update(upd);
})

Actual behavior

throws the error:
@firebase/database: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://.firebaseio.com

!  functions: Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com 
    at fatal (C:\dev\xyz\functions\node_modules\@firebase\database\dist\index.node.cjs.js:341:11)
    at parseRepoInfo (C:\dev\xyz\functions\node_modules\@firebase\database\dist\index.node.cjs.js:1295:9)
    at RepoManager.databaseFromApp (C:\dev\xyz\functions\node_modules\@firebase\database\dist\index.node.cjs.js:14919:25)
    at Object.initStandalone (C:\dev\xyz\functions\node_modules\@firebase\database\dist\index.node.cjs.js:15303:45)
    at DatabaseService.getDatabase (C:\dev\xyz\functions\node_modules\firebase-admin\lib\database\database.js:66:23)
    at FirebaseApp.database (C:\dev\xyz\functions\node_modules\firebase-admin\lib\firebase-app.js:231:24)
    at DataSnapshot.get ref [as ref] (C:\dev\xyz\functions\node_modules\firebase-functions\lib\providers\database.js:279:34)
    at C:\dev\xyz\functions\lib\presense.js:7:31
    at cloudFunction (C:\dev\xyz\functions\node_modules\firebase-functions\lib\cloud-functions.js:132:23)
    at C:\Users\P\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:563:20
!  Your function was killed because it raised an unhandled error.
http

Most helpful comment

The fix has been merged in to https://github.com/firebase/firebase-tools/pull/2434 so this will be fixed completely in the next release of firebase-tools (as long as it's used with firebase-functions 3.8.0 or higher)

All 12 comments

@carrickpaul thanks for reporting this and for the clear reproduction steps, we should be able to fix this but it may require some changes in the Database SDK itself (which seems to be the source of the stack trace)

I was able to reproduce this issue, will dig into the cause.

Hmmm in the debugger I am seeing snap.instance="https://fir-dumpster.localhost" which is definitely not correct (fir-dumpster is my project ID).

Ok @carrickpaul I was able to track down this issue. Fixing it would require changes in both the Functions emulator and the Firebase Functions SDK:

Functions SDK Fix: https://github.com/firebase/firebase-functions/pull/727
Functions emulator fix: https://github.com/firebase/firebase-tools/pull/2434

thanks @samtstern

The fix has been merged in to https://github.com/firebase/firebase-tools/pull/2434 so this will be fixed completely in the next release of firebase-tools (as long as it's used with firebase-functions 3.8.0 or higher)

Thanks a bunch for the fixes posted @samtstern

Even after updating both [email protected], and [email protected], I still get this error, but now from @firebase/database instead (^0.6.0 is pointed to, but still experienced it with a fresh install which pulled 0.6.8 of @firebase/database). This is also being experienced by multiple devs on my team

Here is a snippet of the error:

âš   functions: Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com
    at fatal (/private-code-path/functions/node_modules/@firebase/database/dist/index.node.cjs.js:341:11)
    at parseRepoInfo (/private-code-path/functions/node_modules/@firebase/database/dist/index.node.cjs.js:1295:9)
    at RepoManager.databaseFromApp (/private-code-path/functions/node_modules/@firebase/database/dist/index.node.cjs.js:14985:25)
    at Object.initStandalone (/private-code-path/functions/node_modules/@firebase/database/dist/index.node.cjs.js:15376:45)
    at DatabaseService.getDatabase (/private-code-path/functions/node_modules/firebase-admin/lib/database/database.js:67:23)
    at FirebaseApp.database (/private-code-path/functions/node_modules/firebase-admin/lib/firebase-app.js:232:24)
    at DataSnapshot.get ref [as ref] (/private-code-path/functions/node_modules/firebase-functions/lib/providers/database.js:293:34)
    at ourFunctionName (/private-code-path/)
    at process._tickCallback (internal/process/next_tick.js:68:7)
âš   Your function was killed because it raised an unhandled error.

(NOTE: Paths have been stripped to a base of private-code-path)

Let me know if you need any more info or would like me to open a new issue

I noticed the same, but a release for firebase-tools hasn't been released
since this update, so from my understanding we just need to wait a while
longer

On Thu, 16 Jul 2020, 00:08 Scott Prue, notifications@github.com wrote:

Thanks a bunch for the fixes posted @samtstern
https://github.com/samtstern

Even after updating both firebase-tools, and firebase-functions to the
noted versions, I still get this error, but now from @firebase/database
instead (^0.6.0 is pointed to, but still experienced it with a fresh
install which pulled 0.6.8 of @firebase/database). This is also being
experienced by multiple devs on my team

Let me know if you need any more info or would like me to open a new issue

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/firebase/firebase-functions/issues/726#issuecomment-659040164,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAKRKQTEIPHJD7PX6X7G42LR3YSHRANCNFSM4OUWD5EQ
.

@ElGoorf You are correct - v8.6.0 of firebase-tools was just released a few minutes go and it appears that fixed it. Got confused since I didn't see 2434 mentioned anywhere in the releases

Thanks @samtstern

@prescottprue the fix in the CLI was actually only released a few hours ago in version 8.6.0 ... before that it was merged to master but not released. Can you try with that version?

Oops didn't see your last comment when replying. Glad it works now!

Was this page helpful?
0 / 5 - 0 ratings