1

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!

1
  • data[i][rawSum] != data[i]["rawSum"] (or data[i].rawSum) Commented Jan 12, 2018 at 19:36

1 Answer 1

1

data[i] should be data[keys[i]]. But you can simply loop over the object keys directly, instead of calling Object.keys():

for (key in data) {
    if (data.hasOwnProperty(key)) {
        console.log(data[key].rawSum[interestID]);
        console.log(data[key].rawCount[interestID]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried it the way you suggested and it works! Thank you sir. Since I want to write to "data" and later rewrite the whole object into the database, I left out the if statement. There is no data-set for the given interestID unless I add one in the for-loop now. So as I understand it, I first go into the first node that has the key [key] and then look one level deeper for the node with the key [interestID]? Thanks again for your help.
The purpose of the if statement is to skip properties that are inherited from the object's prototype. See blog.javascripting.com/2014/09/19/…
Ok, I migh have been off the track a bit. It's the first time I'm working with a prototype-based language. Thanks for the clarification, it makes absolutely sense!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.