Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Functions, admin.database().ref(...).get() is not a function

I am working on an Android Application and am using firebase as the back end for it. I am trying to get a notification system working that relies on listening to changes in the database. Having problems though as I am getting the following error. Wondered if anyone would be able to help, any extra code can be supplied.

Firebase Function

'use-strict'

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

exports.sendNotification = functions.database.ref("Notifications/{user_id}/{notification_id}").onWrite((data, context)=>{

    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;

    console.log("User ID : " + user_id + " | Notification ID : " + notification_id);

    return admin.database().ref("Notifications/" + user_id + "/" + notification_id).get().then(queryResult => {

      const job_id = queryResult.data().Job;

      const from_data = admin.database().ref("Jobs/" + job_id).get();
      const to_data = admin.database().ref("Users/" + user_id).get();

      return Promise.all([from_data, to_data]).then(result => {

        const job_name = result[0].data().advertName;
        const to_name = result[1].data().fullName;

        console.log("JOB : " + job_name + " | TO : " + to_name);

        return undefined;
      });


    });
});

Function Error

Function Error

like image 452
Oliver McBurney Avatar asked Jan 03 '23 12:01

Oliver McBurney


2 Answers

It looks like you're mixing up the Realtime Database and Firestore APIs. Firebase Realtime Database APIs doens't provide a get() method to fetch data. For that, you use once() on a Reference object.

like image 99
Doug Stevenson Avatar answered Jan 05 '23 02:01

Doug Stevenson


The error is pretty explicit: there is no get function in the a Firebase Realtime Database.

You seem to be trying to use Cloud Firestore, which is available under admin.firestore(). So something like:

 const from_data = admin.firestore().doc("Jobs/" + job_id).get();
 const to_data = admin.firestore().doc("Users/" + user_id).get();

For full samples see the NODE.JS tab in the Getting started and other Firestore documentation.

like image 33
Frank van Puffelen Avatar answered Jan 05 '23 01:01

Frank van Puffelen