Can't get ref on realtime database with emulator

Viewed 90

I am trying to attach a listener to realtime database on local emulator with another database selected but it's giving me a weird error.

Here is the code I am using:

const functions = require("firebase-functions");

const handler = async (snapshot, context) => {
  console.log(snapshot);
  console.log(context);
}

const target = functions.database
  .instance("test-db")
  .ref("/docs/{docID}")

module.exports = {
  create: target.onCreate(handler),
  update: target.onUpdate(handler),
  delete: target.onDelete(handler),
}

Here is the error that I am getting:

i  functions: Beginning execution of "operationsListenerRealtime-update"
!  functions: TypeError: Cannot read property 'startsWith' of undefined
    at new DataSnapshot (Q:\Projects\test-project\firebase\functions\node_modules\firebase-functions\lib\providers\database.js:270:44)
    at RefBuilder.changeConstructor (Q:\Projects\test-project\firebase\functions\node_modules\firebase-functions\lib\providers\database.js:155:28)
    at cloudFunction (Q:\Projects\test-project\firebase\functions\node_modules\firebase-functions\lib\cloud-functions.js:133:34)
    at C:\Users\duoqu\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:592:16
    at runFunction (C:\Users\duoqu\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:579:15)
    at runBackground (C:\Users\duoqu\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:591:11)
    at processBackground (C:\Users\duoqu\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:574:11)
    at invokeTrigger (C:\Users\duoqu\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:649:19)
    at handleMessage (C:\Users\duoqu\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:736:15)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)
!  Your function was killed because it raised an unhandled error.

Clearly I am trying to connect realtime database that is test-db, I also tried writing http://localhost:7002/?ns=test-db but still no luck. I digged deep into the error and saw this thing:

/**
 * Interface representing a Firebase Realtime Database data snapshot.
 */
class DataSnapshot {
    constructor(data, path, // path will be undefined for the database root
    app, instance) {
        this.app = app;
        if (app && app.options.databaseURL.startsWith('http:')) {
            // In this case we're dealing with an emulator
            this.instance = app.options.databaseURL;
        }
        else if (instance) {
            // SDK always supplies instance, but user's unit tests may not
            this.instance = instance;
        }
        else if (app) {
            this.instance = app.options.databaseURL;
        }
        else if (process.env.GCLOUD_PROJECT) {
            this.instance =
                'https://' + process.env.GCLOUD_PROJECT + '.firebaseio.com';
        }
        this._path = path;
        this._data = data;
    }

I think the issue may be related to this part of the code

Also the instance function takes parameters like this.

enter image description here

EDIT: I have confirmed that with proper node version it still is giving me the same error.

0 Answers
Related