如何在另一个DataSnapshot中访问Firebase数据库值

时间:2017-11-30 13:39:07

标签: javascript firebase firebase-realtime-database google-cloud-functions

以下是我的Firebase数据库的JSON:

[ {
  "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
  }
}

我的目标是创建一个云功能,将selection Firebase数据库节点下的users数组中的数字映射到players数据库节点,以搜索与选择号匹配玩家ID。以下是我尝试创建的云功能:

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');
       });
   });
});

然而,我收到错误消息:

  

TypeError:selection.map不是函数

我怀疑这意味着selection作为变量返回快照中的数组:

snapshot.forEach(function(userSnapshot)

但是在快照中没有这样做:

p.once('value')
      .then(function(dataSnapshot)

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

dataSnapshot侦听器中提供的once()参数将是Admin SDK DataSnapshot的一个实例。要访问此快照下方的子节点,您需要使用child(),例如:

var selection = userSnapshot.child("selection");

这将在给定的子位置提供另一个DataSnapshot实例,因此您的map逻辑应按预期工作。