Q:

How to access a Firebase database value within another DataSnapshot

Below is the JSON of my Firebase database:
[ {
  "R" : 0,
  "Team" : "Industry",
  "Y" : 0,
  "assists" : 0,
  "goals" : 20,
  "id" : 1,
  "image" : "http://res.cloudinary.com/deji/image/upload/v1509140819/industry.jpg_ballers_khc5fy.jpg",
  "name" : "Fabio",
  "position" : "midfielder",
  "price" : 6000000
}, {
  "R" : 0,
  "Team" : "Industry",
  "Y" : 0,
  "assists" : 0,
  "goals" : 8,
  "id" : 2,
  "image" : "http://res.cloudinary.com/deji/image/upload/v1509140819/industry.jpg_ballers_khc5fy.jpg",
  "name" : "Hassan 'Hasi' Akinyera",
  "position" : "defender",
  "price" : 5000000
}, {
  "R" : 0,
  "Team" : "Industry",
  "Y" : 0,
  "assists" : 0,
  "goals" : 0,
  "id" : 3,
  "image" : "http://res.cloudinary.com/deji/image/upload/v1509140819/industry.jpg_ballers_khc5fy.jpg",
  "name" : "Femi 'Fabio' Awoniyi",
  "position" : "defender",
  "price" : 9000000
}
],

users:{
  "1YrpX2W2xnMPoy4YGpZcOE0xJ5g2" : {
    "email" : "muyiw@tmail.com",
    "fullname" : "Muyiz",
    "selection" : [ 1, 2, 3, 4, 5, 6 ],
    "teamname" : "Donawon",
    "total" : 12,
    "userName" : "muyiwatmail.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" : "dam@gmail.com",
    "fullname" : "Dai",
    "selection" : 0,
    "teamname" : "Bayern Neverlosin'",
    "total" : 0,
    "userName" : "dami@gmail.com",
    "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" : "jonan@gmail.com",
    "fullname" : "Join",
    "selection" : [ 40, 8, 10, 24, 18, 34 ],
    "teamname" : "Chad fc",
    "total" : 0,
    "userName" : "jon@gmail.com",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 8,
    "week6" : 0,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  }
}

My aim is to create a Cloud Function that maps the numbers in the selection array under the users Firebase database node unto the players database node in search of matching the selection numbers with the player ids.  Below is that Cloud Function I have tried creating:
exports.sync = functions.https.onRequest((req, res) => {
   const p = admin.database().ref("Player").child("playerweek8").orderByChild("id");
    admin.database().ref('users').once('value').then(function(snapshot) {
       var updates = {};
       snapshot.forEach(function(userSnapshot) {
           var users = userSnapshot.val();
           var selection = users.selection;
           updates[`/users/${userSnapshot.key}/week1`] = 10;
           updates[`/users/${userSnapshot.key}/week2`] = 2;

    p.once('value')
  .then(function(dataSnapshot) {
    // handle read data.
      var p = dataSnapshot.val();
      var normalizedPlayers = p.reduce(function(acc, next) { acc[next.id] = next; return acc; }, {});
            var selectedPlayers = selection.map(function(num){
                return normalizedPlayers[num];
            }); 
            var players = selectedPlayers; 

       });
       admin.database().ref().update(updates).then(function() {
           res.send('it worked');
       });
   });
});

However, then I receive the error message:

TypeError: selection.map is not a function

I suspect that this means that selection as a variable returns the array in the snapshot from:
snapshot.forEach(function(userSnapshot)

But does not do so within the snapshot:
p.once('value')
      .then(function(dataSnapshot)

Is there a way to resolve the issue?

A:

The dataSnapshot parameter provided in the once() listener will be an instance of the Admin SDK DataSnapshot. To access child nodes underneath this snapshot, you'll need to use child(), something like:
var selection = userSnapshot.child("selection");

This will provide another DataSnapshot instance at the given child location, so your map logic should then work as intended.

