javascript - How to fix TypeError: Cannot read property 'push' of undefined - Stack Overflow

I'm trying to iterate through all users in firebase,and create json only from specific params ,bu

I'm trying to iterate through all users in firebase, and create json only from specific params , but i get this error :

TypeError: Cannot read property 'push' of undefined

How can i fix it? Thanks

admin.database().ref("players").once('value', (snapshot, y) => {

        var jsonArray = '{}';

        snapshot.forEach(_child => {

           let cash = _child.child("player_cash");
           let uid = _child.key;
           let name = _child.child("player_name");

            var temp = JSON.parse(jsonArray);
            temp[uid].push({"id":uid,"cash":cash});
            jsonArray = JSON.stringify(temp);
        });

        response.send(jsonArray);
}

I'm trying to iterate through all users in firebase, and create json only from specific params , but i get this error :

TypeError: Cannot read property 'push' of undefined

How can i fix it? Thanks

admin.database().ref("players").once('value', (snapshot, y) => {

        var jsonArray = '{}';

        snapshot.forEach(_child => {

           let cash = _child.child("player_cash");
           let uid = _child.key;
           let name = _child.child("player_name");

            var temp = JSON.parse(jsonArray);
            temp[uid].push({"id":uid,"cash":cash});
            jsonArray = JSON.stringify(temp);
        });

        response.send(jsonArray);
}
Share Improve this question asked Dec 30, 2017 at 12:17 ULAQULAQ 411 gold badge1 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

temp is an empty object. To push to a property of that element, you first have to check whether it exists (and set to an array) or not:

admin.database().ref("players").once('value', (snapshot, y) => {

        var jsonArray = '{}';

        snapshot.forEach(_child => {

           let cash = _child.child("player_cash");
           let uid = _child.key;
           let name = _child.child("player_name");

            var temp = JSON.parse(jsonArray);
            temp[uid] = temp[uid] || [];           // <========
            temp[uid].push({"id":uid,"cash":cash});
            jsonArray = JSON.stringify(temp);
        });

        response.send(jsonArray);
}

I'm not sure why you parse the jsonArray instead of just creating it (immutability?).
Anyway it is an empty object and you try to push to a key inside it. You will need to create the key if its not already there. Maybe you should do it this way:

admin.database().ref("players").once('value', (snapshot, y) => {

        var jsonArray = '{}';

        snapshot.forEach(_child => {

           let cash = _child.child("player_cash");
           let uid = _child.key;
           let name = _child.child("player_name");

            var temp = JSON.parse(jsonArray);
            temp[uid] = temp[uid] || []; // create the key if its not already there
            temp[uid].push({"id":uid,"cash":cash});
            jsonArray = JSON.stringify(temp);
        });

        response.send(jsonArray);
}

The error you are getting is because an Object doesn't have the push method.

You can keep the things simple

admin.database().ref("players").once('value', (snapshot, y) => {
    var jsonArray = {};

    snapshot.forEach(_child => {

       let cash = _child.child("player_cash");
       let uid = _child.key;
       let name = _child.child("player_name");

       jsonArray[uid] = {"id":uid,"cash":cash};
    });

    response.send(jsonArray);
}

The result of this will be:

{
  'id_1': { id: 'id_1', cash: 10 },
  'id_2': { id: 'id_2', cash: 20}
}

Now I suggest you to use an array instead of an object to save the result of the db.

admin.database().ref("players").once('value', (snapshot, y) => {
    var jsonArray = [];

    snapshot.forEach(_child => {

       let cash = _child.child("player_cash");
       let uid = _child.key;
       let name = _child.child("player_name");

       jsonArray.push({"id":uid,"cash":cash});
    });

    response.send(jsonArray);
}

The result of this will be:

[
  { id: 'id_1', cash: 10 },
  { id: 'id_2', cash: 20}
]

This will be easier to handle.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745603057a4635516.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信