【问题标题】:How to refer to an array in a Cloud Function如何在云函数中引用数组
【发布时间】:2018-01-04 16:55:40
【问题描述】:

下面是在我的 Angular JS Webapp 上运行的 javascript 函数:

$scope.pick = [0,1,2,4,5,];
$scope.players = admin.database().ref("Player").child("playerweek8").orderByChild("id");

$firebaseArray(orderedPlayers)
        .$loaded(function(loadedPlayers) {
        var normalizedPlayers = loadedPlayers.reduce(function(acc, next) { acc[next.id] = next; return acc; }, {});
        var selectedPlayers = $scope.pick.map(function(num){
            return normalizedPlayers[num];
        });
        $scope.players = selectedPlayers;

但是,当我尝试将函数转移到 y firebase 数据库 Cloud 函数时,如下所示:

exports.sync = functions.https.onRequest((req, res) => {

    admin.database().ref('users').once('value').then(function(snapshot) {
       var updates = {};
       const orderedPlayers = admin.database().ref("Player").child("playerweek8").orderByChild("id");
       var normalizedPlayers = orderedPlayers.reduce(function(acc, next) { acc[next.id] = next; return acc; }, {});



       snapshot.forEach(function(userSnapshot) {
           var users = userSnapshot.val();
           var selection = users.selection;
           updates[`/users/${userSnapshot.key}/week1`] = 10;


           var players = selection.map(function(num){
                return normalizedPlayers[num];
            }); 
updates[`/users/${userSnapshot.key}/week2`] =   players; 
       });
       admin.database().ref().update(updates).then(function() {
           res.send('it worked');
       });
   });
});

但是,我不断收到错误代码:TypeError: orderedPlayers.reduce is not a function

orderedPlayers 的问题是不是在admin.database().ref("Player").child("playerweek8").orderByChild("id"); 返回播放器数组?

【问题讨论】:

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


    【解决方案1】:

    查看 API 文档,orderByChild() 是 Query 上的一个方法,它返回另一个 Query。它不包含任何结果。

    如果您想要查询结果,则必须对其调用once(),然后使用生成的快照来迭代结果。

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2017-11-02
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多