0

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?

1
  • Does the string have any leading / trailing whitespace? Try var itemkey = window.localStorage.getItem('fbid').trim(); Commented Sep 9, 2015 at 6:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.