Home > Software engineering >  How to use the return of a cloud function in realtime databse [Firebase]
How to use the return of a cloud function in realtime databse [Firebase]

Time:05-13

I am using Realtime database from Firebase.

I have a cloud function in my index.js that returns an object:

{"state":"create","users":["user1","user2"]}

I would like to get it from my java code to use it later but I don't know how to proceed..

Here is my cloud function:

exports.getProductTypeUpdate = functions.database
.ref("Products/{product}/type")
.onUpdate((snapshot, context) => {
  // on récupère le type du produit
  const type = snapshot.after.val();
  // on récupère les utilisateurs interresse par le type d'objet
  admin.database().ref("Notif/type/"   type)
      .once("value").then((snapshot) => {
        const users = snapshot.val();
        // Retourne les utilisateurs à notifier et l'état de la requête
        const result = {state: "update", users: users};
        console.log("Le résultat final est: "   JSON.stringify(result));
        return result;
      }).catch((error) => {
        console.log("Error sending message:", error);
        return false;
      });
});

CodePudding user response:

I have a cloud function in my index.js that returns an object... I would like to get it from my java code to use it later.

All Realtime Database Cloud Functions are backgound triggered Cloud Functions (incl. .onUpdate) which means that they don't communicate with any other component of your application (e.g. you Android front-end app or a server you own running java code).

If you want to send back the result of the Cloud Function to your Android front-end application, there are several possibilities, depending on your exact business case:

  • Call a Callable Cloud Function from your Android application, which does the write to the RTDB and returns back the result (preferred approach IMHO);
  • Set a listener to the node of the RTDB node on which you write the result (note that it will cost one write).

Side note: I suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/.

In particular the videos explain why you must return the Promise chain in your Cloud Function:

exports.getProductTypeUpdate = functions.database
.ref("Products/{product}/type")
.onUpdate((snapshot, context) => {
  // on récupère le type du produit
  const type = snapshot.after.val();
  // on récupère les utilisateurs interresse par le type d'objet
  return admin.database().ref("Notif/type/"   type)   // <= !!! See return here
      .once("value").then((snapshot) => {
        const users = snapshot.val();
        // Retourne les utilisateurs à notifier et l'état de la requête
        const result = {state: "update", users: users};
        console.log("Le résultat final est: "   JSON.stringify(result));
        return result;
      }).catch((error) => {
        console.log("Error sending message:", error);
        return false;
      });
});
  • Related