1

EDIT: it seems that value[i].extrainfoimage is only undefined within imageRef.child("-JlSvEAw......

I've read through the documentation twice so far and still haven't been able to figure this one out.

Basically I load some data from firebase and set it as an array. I iterate through that array and intend to replace some properties, as I iterate, with data I get from other places in my Firebase DB. However everytime I go to replace the properties I get

"Uncaught TypeError: Cannot set property 'extrainfoimage' of undefined"

This is my code:

var questionsRef = new Firebase("https://XX.firebaseio.com/Questions");
var imageRef = new Firebase("https://XX.firebaseio.com/Images");

//get the first 6 items
var query = questionsRef.orderByChild('date_time_asked').limitToFirst(6);
//get them as a firebase array promise
$scope.questions = $firebaseArray(query);
//wait for it to load
$scope.questions.$loaded().then(function (value) {
//iterate through
for (i = 0; i < value.length; i++) {
    //check if there is data to be replaced
    if (value[i].extrainfoimage) {
           //if there is fetch it from firebase and replace
           imageRef.child("-JlSvEAwu5-WZJkOE_b/image").once("value",function(data){
                        value[i].extrainfoimage = data.val();
                    });
                }
            }
        })
2
  • $scope.questions.$loaded().then(function (value) { This is returning value to be undefined. And you're trying to set extrainfoimage of undefined that's why you're getting that error. Commented Mar 28, 2015 at 4:33
  • No it's not. I wouldn't be entering my if statement if it was Commented Mar 28, 2015 at 11:45

1 Answer 1

1

Possibly its becuase the last item in value doesn't have extrainfoimage. Because your set for value[i].extrainfoimage is async, it doesn't capture the correct i value, and therefore fails when it is executing.

Try to wrap it in a IIFE:

for (i = 0; i < value.length; i++) {
    //check if there is data to be replaced
    if (value[i].extrainfoimage) {
       (function(curr) {
           //if there is fetch it from firebase and replace
           imageRef.child("-JlSvEAwu5-WZJkOE_b/image").once("value",function(data){
                    value[curr].extrainfoimage = data.val();
           });
        })(i);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. Don't think I would have been able to figure this one out on my own. For some reason it wouldnt actually update the image until I switched to a different page and then went back to the page (I think this was more of an ionic issue than anything else) so I added $ionicSlideBoxDelegate.update(); right after i changed the property and that seemed to fix it.

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.