7
votes

I am new to firebase but when I deploy my functions and execute onWrite() method this is the error I get:

ReferenceError: firebase is not defined at exports.sendJobNotification.functions.database.ref.onWrite.event (/user_code/index.js:11:3) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20 at process._tickDomainCallback (internal/process/next_tick.js:135:7)


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

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendPaymentNotification = functions.database.ref('payments/{paymentID}').onWrite(event => {
    if (event.data.previous.exists()) {
        return;
      }

      firebase.database().ref('payments').child(event.params.paymentID).once('value').then(function(snap){
            var jobData = snap.val();
            console.log(jobData)
      });
});
2
Don't you mean to use admin instead of firebase? e.g. admin.database().ref()...Arnelle Balane
Thanks. it works with adminFredrick Ochieng
Thanks this helped me too. But whats weird is that in my Javascript file in my web development i never needed to do that. Its only in the javascript file in cloudFunctions that i have to put admin.database().....user3833732

2 Answers

2
votes

For anyone googling that. You should use "admin" instead of "firebase".

1
votes

Add:

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

in your requires at the start of the function js file, and ensure you use admin in place of firebase when initializing a connection.

For example:

Use: admin.firestore().collection()

Instead of: firebase.firestore().collection()