【问题标题】:How to access a Firebase database value within another DataSnapshot如何访问另一个 DataSnapshot 中的 Firebase 数据库值
【发布时间】:2017-11-30 13:39:06
【问题描述】:

下面是我的 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
  }
}

我的目标是创建一个云函数,将users Firebase 数据库节点下的selection 数组中的数字映射到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)

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-12-12
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      相关资源
      最近更新 更多