最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far