[英]Why can't I access properties in a nested JS Object? (Firebase Snapshot)
TLDR: please scroll to part after JSON-snippet ;) TLDR:请滚动到JSON片段后的部分;)
I'm currently working on a Firebase-Project. 我目前正在研究Firebase-Project。 I'm writing code in JavaScript.
我正在用JavaScript编写代码。 Since hours I try to extract data from / write into an object, but for some reason I can't access it's params.
数小时以来,我尝试从对象中提取数据/将数据写入对象,但是由于某些原因,我无法访问它的参数。
Initially my code looked as follows: 最初,我的代码如下所示:
exports.updateUsersNewInterest = functions.database.ref('/category/{categoryID}/interest/{interestID}').onCreate(event =>{
const interestID = event.params.interestID;
const categoryID = event.params.categoryID;
var ref = admin.database().ref("/userInterests/");
ref.once("value").then(function(snapshot){
snapshot.forEach(function(childSnapshot){
var userID = childSnapshot.key;
childSnapshotVal = childSnapshot.val();
var rawSum = childSnapshotVal.rawSum;
var rawCount = childSnapshotVal.rawCount;
var norm = childSnapshotVal.norm;
rawSum[interestID] = 0;
rawCount[interestID] = 0;
var resultObject = {};
resultObject.norm = norm;
resultObject.rawCount = rawCount;
resultObject.rawSum = rawSum;
var ref1 = admin.database().ref("userInterests/"+userID);
return ref1.set(resultObject);
})
return true
})
return true
})
Since for some reason I wasn't able to read the keys out of the single childSnapshot objects in the forEach, I had to use another attempt: 由于某种原因,我无法从forEach中的单个childSnapshot对象中读取密钥,因此我不得不尝试另一种方法:
exports.updateUsersNewInterest = functions.database.ref('/category/{categoryID}/interest/{interestID}').onCreate(event =>{
const interestID = event.params.interestID;
const categoryID = event.params.categoryID;
//console.log(categoryID);
//console.log(interestID);
var ref = admin.database().ref("/userInterests/");
ref.once("value").then(function(snapshot){
var data = snapshot.val();
var keys = Object.keys(data);
console.log(typeof snapshot);
for (var i = 0; i < keys.length; i++){
//At this point I cant access any properties
//console.log(data[i][rawSum] for example is NOT working
// data[i][rawSum][interestID] = 0;
// data[i][rawCount][interestID] = 0;
}
var ref1 = admin.database().ref("userInterests/");
return ref1.set(data);
})
return true
})
Now the thing is, when I try to console.log the key inside the for-loop, I get the right results. 现在的事情是,当我尝试在for循环中使用console.log键时,我得到了正确的结果。 But performing any kind of action related to the properties of either $data or $snapshot (even though var data = snapshot.val() should give me an object?! is not working.
但是执行与$ data或$ snapshot的属性相关的任何类型的操作(即使var data = snapshot.val()应该给我一个对象?也不起作用)。
I might have lost some brackets copying the code from sublime to here but the general problem stays the same, even if my code-snippet is not complete here. 将代码从崇高复制到此处可能会失去括号,但是即使我的代码段不完整,一般的问题仍然存在。
The firebase console gives me the error: Firebase控制台给我错误:
TypeError: Cannot read property 'rawSum' of undefined at
The object looks as follows: 该对象如下所示:
{ '2wewe':
{ rawCount:
{ '11': 1,
'17': 0,
'18': 0,
'19': 0,
'33': 0,
'35': 0,
'36': 0,
'40': 0 },
rawSum:
{ '11': 1,
'17': 0,
'18': 0,
'19': 0,
...
So if I export a snapshot from Firebase and then extract its values via 因此,如果我从Firebase导出快照,然后通过以下方式提取其值
var values = snapshot.val()
I should be able to eg writh into it with 我应该能够例如
var abc = values[id]['rawCount'][itemID]
or what am I missing? 还是我想念什么?
And why is 为什么
snapshot.forEach(function(childSnapshot){
var userID = childSnapshot.key;
just giving me "undefined"? 只是给我“未定义”?
Would really appreciate any clue for my (probable rookie) problem. 真的很感谢我(可能是菜鸟)问题的任何线索。
Thanks in advance! 提前致谢!
data[i]
should be data[keys[i]]
. data[i]
应该是data[keys[i]]
。 But you can simply loop over the object keys directly, instead of calling Object.keys()
: 但是您可以直接直接遍历对象键,而不用调用
Object.keys()
:
for (key in data) {
if (data.hasOwnProperty(key)) {
console.log(data[key].rawSum[interestID]);
console.log(data[key].rawCount[interestID]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.