admin管理员组文章数量:1433506
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
3 Answers
Reset to default 2temp 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
版权声明:本文标题:javascript - How to fix TypeError: Cannot read property 'push' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745603057a2665681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论