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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With