I am following the Thinkster Ang-News tutorial with the latest version of AngularFire (0.8.0) installed. I've managed to get to part 7 of the tutorial successfully but I am stuck on the part where I need to insert a user object into the Firebase Forge. The code works as follows:
When a new user registers via my HTML form, the "register" function is called and "User.create(authUser, $scope.user.username);" is executed within the "register" function.
// login and registration page
app.controller('AuthCtrl',
function ($scope, $location, Auth, User) {
if (Auth.signedIn()) {
$location.path('/');
}
$scope.$on('firebaseSimpleLogin:login', function(){
$location.path('/');
});
$scope.login = function(){
Auth.login($scope.user).then(function(){
$location.path('/');
}, function (error){
$scope.error = error.toString();
});
};
$scope.register = function () {
Auth.register($scope.user).then(function (authUser) {
Auth.login($scope.user);//cause creating user auto login
User.create(authUser, $scope.user.username);
console.log(authUser);
$location.path('/');
}, function (error){
$scope.error = error.toString();
});
};
});
The "User" service has a function "create" which creates the user object and should store the object information in the Firebase Forge. As you can see below, "users.$save(username)" is called to "$save" the user into the Firebase Forge. However, nothing happens in my Firebase Forge when the $save method is called. I reviewed the latest AngularFire specs, and as you can see in my code comments (//), I tried combining the $asArray(), $asObject() with my "users" variable but that did not work. When I used "users.$add(username);" instead of calling the $save method, my Firebase Forge just added the username without the object's properties.
app.factory('User', function ($firebase, FIREBASE_URL, Auth, $rootScope) {
var ref = new Firebase(FIREBASE_URL + 'users');
var users = $firebase(ref);
//var users = $firebase(ref).$asObject();
//var users = $firebase(ref).$asArray();
var User = {
create: function (authUser, username) {
users[username] = {
md5_hash: authUser.md5_hash,
username: username,
$priority: authUser.uid
};
//users.$add(username);
users.$save(username).then(function () {
setCurrentUser(username);
});
}, // end of create fxn
Again, I cannot insert the "users[username]" object into my Firebase Forge with the $save or $add AngularFire methods. I would greatly appreciate any help.
Jon
Right now you're modifying the binding and then calling $save
. You have to use $asObject
or $asArray
to get to the $save
method.
In your case though, you don't need to sync any objects—just save one. You can create a new object for the user and use $update
on the binding.
app.factory('User', function ($firebase, FIREBASE_URL, Auth, $rootScope) {
var ref = new Firebase(FIREBASE_URL + 'users');
var users = $firebase(ref);
var User = {
create: function (authUser, username) {
users.$update(username, {
md5_hash: authUser.md5_hash,
username: username,
priority: authUser.uid
});
}, // end of create fxn
Following on the previous answer, I used this $update syntax to set the priority, which appears to work:
users.$update(username, {
md5_hash : authUser.md5_hash,
username : username
}).then(function(ref) {
ref.setPriority(authUser.uid);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With