title: How to access a Firebase database value within another DataSnapshot
body: <p>Below is the JSON of my Firebase database:</p>
  
  <pre><code>[ {
    "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
    }
  }
  </code></pre>
  
  <p>My aim is to create a Cloud Function that maps the numbers in the <code>selection</code> array under the <code>users</code> Firebase database node unto the <code>players</code> database node in search of matching the selection numbers with the player ids.  Below is that Cloud Function I have tried creating:</p>
  
  <pre><code>exports.sync = functions.https.onRequest((req, res) =&gt; {
     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');
         });
     });
  });
  </code></pre>
  
  <p>However, then I receive the error message:</p>
  
  <blockquote>
    <p>TypeError: selection.map is not a function</p>
  </blockquote>
  
  <p>I suspect that this means that <code>selection</code> as a variable returns the array in the snapshot from:</p>
  
  <pre><code>snapshot.forEach(function(userSnapshot)
  </code></pre>
  
  <p>But does not do so within the snapshot:</p>
  
  <pre><code>p.once('value')
        .then(function(dataSnapshot)
  </code></pre>
  
  <p>Is there a way to resolve the issue?</p>