admin管理员组

文章数量:1023794

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.

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.

本文标签: javascriptHow to fix TypeError Cannot read property 39push39 of undefinedStack Overflow