0
votes

I aim to have a databse trigger such that when there is a change in the "Players" databse node, there will be a corresponding set () function at a different firebase node called users.

Below is the cloud function I have been working on:

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.update = functions.database.ref('/Player')
    .onWrite(event=>{
      console.log(event.data);
      var ref = admin.database().ref(`/users/{user.uid}/week1`);
      ref.set(10);
      return;

    });

The issue is that the cloud function returns the error:

Error: Firebase.child failed: First argument was an invalid path: "/users/{user.uid}/week1". Paths must be non-empty strings...

There are multiple users as follows so is there a way I can access what the user uid will be for each user and refer to this within me cloud function?:

 users: {
   user uid (generated by push) : {
       deviceToken : "tokenID",
       name : "Display Name"
   },
   anotherUserID : {
       deviceToken : "tokenID",
       name : "Display Name"
   }

The Players node is as follows :

{
"players" : [
       {"name" : "Yasin 'YB' Amusan", 
        "Team" : "Industry",
        "price": 8000000, 
        "position": "forward", 
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png", 
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Hassan 'Hasi' Akinyera",
        "Team" : "Industry", 
        "price": 5000000, 
        "position": "defender",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0,
        "Y" : 0,
        "R" : 0},
       {"name" : "Femi 'Fabio' Awoniyi",
        "Team" : "Industry",
        "price": 9000000,
        "position": "defender",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Deji 'Dej' Awoniyi",
        "Team" : "Industry",
        "price": 7000000,
        "position": "forward",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Koye 'K10' Kekere-Ekun",
        "Team" : "Industry",
        "price": 9000000,
        "position":"midfielder",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Teni 'Teezee' Zacchaeus",
        "Team" : "Industry",
        "price": 6000000, 
        "position":"hybrid",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Bolaji 'Boj' Odojukan",
        "Team" : "Industry",
        "price": 7000000,
        "position":"forward",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Ernest",
        "Team" : "Industry",
        "price": 6000000,
        "position":"defender"
}
1
Right now you're trigger on /Player, so there is no concept on a user.uid. What does the Player node look like? - Frank van Puffelen
@FrankvanPuffelen hey. I've edited the question to show the Player node. Regarding your comment I now see that there is no concept of a user.uid in the Player node so how will i access the database reference I aim to access re: admin.database().ref(/users/{user.uid}/week1); - Kola Ayanwale
I'm having a hard time understanding your use-case and thus providing help. Maybe somebody else understands it well enough to help. - Frank van Puffelen

1 Answers

0
votes

There are a couple issues with your function, I will not rewrite the function for you, but will point out some things.

First, I suggest taking a look at these docs: https://firebase.google.com/docs/functions/get-started

  1. Your trigger reference is not correct. Your database node is "players" not "Player"
  2. Your var ref is not correct either. Are you trying to insert the user id from a variable that is not in your code? You will need to know the user's id before you can set values.

Edit: I suggest adding the user uid to the players ? Then on function execution you can access the user uid by doing something similar to this:

var player = event.data.val();
var uid = player['useruid'];

It is a little hard to tell what you are after.