7

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)
      });
});
3
  • 6
    Don't you mean to use admin instead of firebase? e.g. admin.database().ref()... Commented Aug 30, 2017 at 8:18
  • 1
    Thanks. it works with admin Commented Aug 30, 2017 at 8:47
  • 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()..... Commented Feb 9, 2018 at 13:47

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.