Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: firebase is not defined in cloud functions

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)
      });
});
like image 612
Fredrick Ochieng Avatar asked Aug 30 '17 07:08

Fredrick Ochieng


People also ask

Why firebase is not defined?

This means that you didn't include the Firebase JavaScript library in your page yet. Instead of starting from a blog post, I recommend that you start with this 5-minute interactive tutorial. It explains what to add in the first step.

How do I find my firebase database URL?

You can find your Realtime Database URL in the Realtime Database section of the Firebase console. Depending on the location of the database, the database URL will be in one of the following forms: https:// DATABASE_NAME . firebaseio.com (for databases in us-central1 )

How is firebase?

Firebase is a Backend-as-a-Service (Baas). It provides developers with a variety of tools and services to help them develop quality apps, grow their user base, and earn profit. It is built on Google's infrastructure. Firebase is categorized as a NoSQL database program, which stores data in JSON-like documents.


2 Answers

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

like image 156
Daniel Avatar answered Sep 30 '22 22:09

Daniel


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

like image 23
David Avatar answered Sep 30 '22 21:09

David