如何引用云函数中的数组

时间:2018-01-04 16:55:41

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

以下是在我的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数据库云函数时,如下所示:

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");返回播放器数组?

1 个答案:

答案 0 :(得分:0)

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

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