I've been encountering an issue while trying to insert data into my Firebase database using a user's uid
. It seems to be coming out as undefined for some reason. Here's a closer look at the code snippet causing the problem:
The primary controller that initializes the user's data information (authData
) when the page loads:
flickrApp.controller('mainCtrl', ['$scope', '$rootScope', '$firebase', 'Auth', 'shared', function($scope, $rootScope, $firebase, Auth, shared) {
Auth.$onAuth(function(authData) {
shared.setAuth(authData);
$scope.authData = shared.getAuth();
});
}]);
The service responsible for managing authentication state and sharing it across controllers:
flickrApp.service('shared', function() {
var authentication = false;
return {
getAuth: function () {
return authentication;
},
setAuth: function (auth) {
authentication = auth;
}
};
});
The problem arises in my tags controller where things don't seem to work as expected. Although the $scope.authData
is correctly set in the $watch
function, attempting to use it in the var ref
line results in it being flagged as undefined (preventing me from accessing the uid
). I'm struggling to identify why this discrepancy exists...
Should I consider implementing $apply
within the watcher function or is there another underlying issue?
flickrApp.controller('tagsCtrl', ['$scope', '$rootScope', '$firebase', 'shared', function($scope, $rootScope, $firebase, shared) {
$scope.tagsList = [];
$scope.shared = shared;
$scope.$watch('shared.getAuth()', function(authData) {
$scope.authData = authData;
console.log($scope.authData);
});
var ref = new Firebase ('https://flickr.firebaseio.com/users/' + $scope.authData.uid);
var sync = $firebase(ref);
$scope.addTag = function(tag) {
$scope.tagsList.push(tag);
sync.$set({favoriteTags: $scope.tagsList});
}
}]);