DGonzo
DGonzo

Reputation: 11

How to solve сannot read property 'databaseURL' of null in Cloud Functions for Firebase?

I have a table titled 'bestOffers' in Cloud Firestore I am using a function that calls when a document is added to a table.

The event settings are like this: Event type: create The path to the document: bestOffers/{id}

Function: enter image description here

And when I run the function, I get a cannot read property 'databaseURL' of null error

Can you please tell me what am I doing wrong?

Code:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp()

exports.sendNotification = functions.database.ref('bestOffers/{id}').onWrite(async (change, context) => {
 

})

Tracing:

TypeError: Cannot read property 'databaseURL' of null
    at resourceGetter (/workspace/node_modules/firebase-functions/lib/providers/database.js:92:54)
    at cloudFunctionNewSignature (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:102:13)
    at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:151:20)
    at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:198:28)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Upvotes: 0

Views: 412

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83163

Have a look at the signature of the handler function that is passed to onWrite. It has two parameters: a Change<DataSnapshot> and an EventContext.

You declare your Cloud Function with:

...onWrite(async () => {...});

You should do as follows:

.onWrite(async (change, context) => {
  const beforeData = change.before.val(); // data before the write
  const afterData = change.after.val(); // data after the write
});

and, then, use the change object to get the data stored at the node that triggered the Cloud Function.

Upvotes: 1

Related Questions