Alexsandra Guerra
Alexsandra Guerra

Reputation: 1665

Access $firebaseObject value with variable instead of string

I need to assign a variable to a string and pass that variable to a Firebase query in a $firebaseObject. But, when I try this, the firebaseObject is null. I tried with directly putting the string in and it works, but I don't want that.

Code:

//var itemkey='-JyO7Zsgsucf5ESttJwt';
var itemkey=window.localStorage['fbid'];-------> string: '-JyO7Zsgsucf5ESttJwt'
console.log(typeof itemkey); ------------------> string
$scope.galphoto = $firebaseObject(ref.child(itemkey));
console.log($scope.galphoto) ------------------> null

When I call

$scope.galphoto = $firebaseObject(ref.child(itemkey));

With itemkey='-JyO7Zsgsucf5ESttJwt',

then console.log($scope.galphoto) is properly shown. However, when I use what I really want to use,

window.localStorage['fbid']

then console.log($scope.galphoto) is null.

Why?

Upvotes: 0

Views: 442

Answers (1)

Jeremy Belolo
Jeremy Belolo

Reputation: 4539

I think you may be trying to console.log the result before it's available. $firebaseObject returns the data with some delay. You may try to do something like

$scope.galphoto = $firebaseObject(ref.child(itemkey));
$scope.galphoto.$loaded().then(function () {
    console.log($scope.galphoto);
}

You should see the expected result, since you're waiting for the promise to respond. It could help you better understand how to get the desired result.

Upvotes: 1

Related Questions