So I have my AngularJs + AngularFire with email/password Authentication working, also have a node called "users" where I can list all the users authenticated to my app, as you can see:
So, in my authentication service I have an AngularJs factory:
.factory('Auth', function($firebaseAuth, $firebaseObject, $firebaseArray){
var ref = firebase.database().ref();
var auth = $firebaseAuth();
var users = $firebaseArray(ref.child('users'));
var firebaseUser = auth.$getAuth();
var Auth = {
getStudent: function(studentId){
console.log(studentId)
return $firebaseObject(ref.child('users').child(studentId));
},
studentSignupProfile: function(profile){
console.log('Student profile service..!');
var obj = Auth.getStudent(firebaseUser.uid);
console.log(obj.name);
return obj.$loaded().then(function(){
obj.phone = profile.phone;
obj.address = profile.address;
return obj.$save();
});
}
};
return Auth;
});
This is my controller:
$scope.studentSignupProfile = function(profile){
console.log(profile);
if (profile == undefined) {
toaster.pop('info','Deseamos saber más acerca de tí!');
} else {
Auth.studentSignupProfile(profile).then(function(msg){
toaster.pop('info','Perfil actualizado!');
$state.go('home');
});
}
}
The response is undefined in console.log(obj.name). And as you can see the user UID exist (picture above).
In my getStudent function there is a console.log(studentId) that show me the exact uid => 4BAIdOXZv6NTEKsjgI15JTajFtz2. But when I tried to grab the $firebaseObject it show me undefined.
You are writing to the console before your object has loaded. That console.log()
should be inside the $loaded
handler so it will wait before trying to write the name:
var obj = Auth.getStudent(firebaseUser.uid);
return obj.$loaded().then(function(){
// object now loaded
console.log(obj.name);
obj.phone = profile.phone;
obj.address = profile.address;
return obj.$save();
});