Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database ref from trigger throws error on firebase function emulator #726

Closed
carrickpaul opened this issue Jul 2, 2020 · 12 comments · Fixed by firebase/firebase-tools#2434
Assignees

Comments

@carrickpaul
Copy link

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.
@carrickpaul carrickpaul changed the title Database ref from trigger throws error on emulator Database ref from trigger throws error on firebase function emulator Jul 2, 2020
@samtstern
Copy link
Contributor

@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)

@samtstern samtstern assigned yuchenshi and samtstern and unassigned yuchenshi Jul 7, 2020
@samtstern
Copy link
Contributor

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

@samtstern
Copy link
Contributor

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

@samtstern
Copy link
Contributor

Ah this is actually a bug in the firebase-functions SDK:

export function extractInstanceAndPath(

We can see that this line will produce the awkward .localhost domain:

const dbInstance = 'https://' + dbInstanceName + '.' + domain;

@samtstern
Copy link
Contributor

samtstern commented Jul 8, 2020

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: #727
Functions emulator fix: firebase/firebase-tools#2434

@carrickpaul
Copy link
Author

thanks @samtstern

@samtstern
Copy link
Contributor

The fix has been merged in to firebase/firebase-tools#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)

@prescottprue
Copy link

prescottprue commented Jul 15, 2020

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

@HDv2b
Copy link

HDv2b commented Jul 15, 2020 via email

@prescottprue
Copy link

prescottprue commented Jul 15, 2020

@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

@samtstern
Copy link
Contributor

samtstern commented Jul 16, 2020

@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?

@samtstern
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants