Kola Ayanwale
Kola Ayanwale

Reputation: 175

Firebase Cloud Functions forEach()

So Below is the JSON for my firebase databse:

{
  "1YrpX2W2xnMPoy4YGpZcOE0xJ5g2" : {
    "email" : "[email protected]",
    "fullname" : "Muyiz",
    "selection" : [ 1, 2, 3, 4, 5, 6 ],
    "teamname" : "awon",
    "total" : 12,
    "userName" : "motmail.com",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 0,
    "week6" : 12,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  },
  "6K9rQiZQ3jaV38WWtDbNwxhqIwc2" : {
    "email" : "[email protected]",
    "fullname" : "Dai",
    "selection" : 0,
    "teamname" : "Bayern Neverlosin'",
    "total" : 0,
    "userName" : "[email protected]",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 0,
    "week6" : 0,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  },
  "9OgN4HyMtARaQEQV1mKQ5lyE1992" : {
    "email" : "jonail.com",
    "fullname" : "Jon",
    "selection" : [ 40, 8, 10, 24, 18, 34 ],
    "teamname" : "Chad fc",
    "total" : 0,
    "userName" : "[email protected]",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 8,
    "week6" : 0,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  },
  "AGVZAUye5ZbZgvwCOpMeDkoOsEU2" : {
    "email" : "[email protected]",
    "fullname" : "Emeka Iheme",
    "selection" : 0,
    "teamname" : "Young Money",
    "total" : 0,
    "userName" : "[email protected]",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 0,
    "week6" : 0,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  }
}

Below is the cloud function I use to that has kind of worked :

exports.update = functions.database.ref('/users/{user.uid}')
    .onWrite(event=>{
   console.log ('it banged!');

   const uid = event.data.key;


   ref = admin.database().ref(`/users/`+ uid + `week1`);
   pref1 = admin.database().ref("Player").child("playerweek8");
   ref2 = admin.database().ref(`/users/` + uid `/` );
   if(n === 4){

       ref.set(10);
}

The ISSUE

The issue is that the code only work when there is a change in the respective user. For example, if I change a write in the user with uid 1YrpX2W2xnMPoy4YGpZcOE0xJ5g2 it sets 10 to the ref. But what I had aimed for is a change to the user node to result in ref.set(10) for each of the users. Is that not possible? Sorry as you could probably guess, I am new to firebase. Thanks in advance.

Upvotes: 1

Views: 3712

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598951

If you are new to Firebase and JavaScript, Cloud Functions for Firebase is not the best way to learn it. I recommend first reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer. They cover many basic JavaScript, Web and Firebase interactions. After those you'll be much better equipped to write code for Cloud Functions too. But I'll try to explain why your code doesn't/can't work and how I'd approach it below.

You're using a database trigger, which only gets invoked when there is a write to the database that matches the criteria you specify. So your:

exports.update = functions.database.ref('/users/{user.uid}').onWrite(

Only triggers when there is a write to a user node.

If you want to do batch processing of all users, you'll probably want use a HTTP trigger:

exports.updateUsers = functions.https.onRequest((req, res) => {
  ...
});

When you deploy this function, it give you a URL. You can trigger the function by calling the URL, or by simply opening it in a browser.

In the function you'll want to load the users from the database, and process them. The total will leave you with something like:

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

exports.updateUsers = functions.https.onRequest((req, res) => {
  admin.database().ref("users").once("value").then((snapshot) => {
    snapshot.forEach((userSnap) => {
      userSnap.child("week1").set(10);
    });
  });
});

You'll need to modify the code in the function to your needs. I mostly focused on showing the main concepts: triggering via HTTP, and then loading data from the database.

Upvotes: 4

Related Questions