Reputation: 1226
I ran into an issue here. The piece of code below is to check user status and then return the firstname and other information on the app. Currently I can't access firstname and other objects thrown back from firebase database. Is there a different method to use or am I doing something illegal here? I have been struggling for sometimes. Any help or suggestions is welcomed.
myApp.controller('StatusController',
function ($scope, $rootScope, $firebaseAuth, FIREBASE_URL, $location, Authentication) {
$scope.logout = function(){
Authentication.logout();
$location.path('/login');
}
var authData = new Firebase(FIREBASE_URL + '/users/');
var syncAuthData = $firebaseAuth(authData).$asObject();
if (syncAuthData) {
$scope.currentUser = syncAuthData; // Here currentUser should access firstname and etc
console.log("Logged in as:", syncAuthData);
} else {
console.log("Logged out");
$scope.currentUser = null;
}
////var authData = syncAuthData.$getAuth();
//if (syncAuthData) {
// $scope.currentUser = syncAuthData;
// console.log("Logged in as:", syncAuthData);
//} else {
// console.log("Logged out");
// $scope.currentUser = null;
//}
});//Statuscontroller
Upvotes: 0
Views: 107
Reputation: 11931
There appear to be two potential problems in your code sample:
If you want to query out data, I believe you need to use $firebase(ref).$asObject()
not $firebaseAuth(ref).$asObject()
. The AngularFire API Docs for $firebaseAuth do not suggest this use. What sample or documentation are you following for that?
Second, Firebase data comes back asynchronously, so your code to log the data may not have the results at the time it runs. There is a $loaded() method that returns a promise you can use to attach a callback for when the data arrives.
With both changes, the code might look something like this:
var usersRef = new Firebase(FIREBASE_URL + '/users/');
var syncObj = $firebase(usersRef).$asObject().$loaded()
.then(function (myData) {
console.log("My Firebase data is: ", myData);
});
I am not very familiar with Firebase authentication, perhaps someone can chime in with a sample for how to get properties from the active user?
Upvotes: 2