TLDR: please scroll to part after JSON-snippet ;)
I'm currently working on a Firebase-Project. I'm writing code in 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:
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. 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.
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:
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
var values = snapshot.val()
I should be able to e.g. 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][rawSum]!=data[i]["rawSum"](ordata[i].rawSum)