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

无人久伴 提交于 2021-01-27 14:16:23

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/49901729/firebase-functions-admin-database-ref-get-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!