0

Here is the task. When a user is created, I would like to add an instance of them in the main database so they can then have things associated with them (lists and recipes). I tried doing .set() and .update() and while both added an instance of the user in my DB, it would get overwritten each time a new user registered. How would I go about not having that information overwritten? I will keep searching and update my post based on the information I have found. Here is the auth code I have.

app.controller("SampleCtrl", ["$scope", "Auth",
  function($scope, Auth) {
    $scope.auth = Auth;

    $scope.createUser = function() {
      $scope.message = null;
      $scope.error = null;

      // Create a new user
      Auth.$createUserWithEmailAndPassword($scope.email, $scope.password, $scope.name)
        .then(function(firebaseUser) {
          $scope.message = "User created with uid: " + firebaseUser.uid;
          const uid = firebaseUser.uid;
          const dbref = firebase.database().ref().child('users');
          dbref.update({
            id: uid,
            name: $scope.name
          });
            console.log(dbref.uid);
        }).catch(function(error) {
          $scope.error = error;
        });
    };

    $scope.deleteUser = function() {
      $scope.message = null;
      $scope.error = null;

      $scope.auth.$onAuthStateChanged(function(firebaseUser) {
      $scope.firebaseUser = firebaseUser;
    });

      // Delete the currently signed-in user
      Auth.$deleteUser().then(function() {
        $scope.message = "User deleted";
      }).catch(function(error) {
        $scope.error = error;
      });
    };
  }
]);

Thanks in advance!

******EDIT**********

Okay, so I got information to be added, but not in the way I expected. I want the structure to be like this users ----- id -------info Where id is the key and info is the value that holds a new object. What I am getting is this... a random serialized ID and then my user id and information object

The question is now, how do I get rid of that random serialized key?

maxsands1503
  • 157
  • 2
  • 15

2 Answers2

1

The problem here is you are writing directly to the users node but you want every user to have their own node. You can fix this by writing one level deeper like this:

const dbref = firebase.database().ref().child('users').child(uid);
dbref.update({
  name: $scope.name
});

You can also take a look at this answer i wrote for more details surrounding this entire process.

Community
  • 1
  • 1
André Kool
  • 4,880
  • 12
  • 34
  • 44
1

The problem is that you need to put each user under a form for key. Now you update the users object with new data, and then it overwrites. Thats how firebase works.

So if you do something like this, then run update

const dbref = firebase.database().ref().child('users').child(userid);
dbref.update({
   name: $scope.name
});

Or

const dbref = firebase.database().ref('users/'+userid);
dbref.update({
   name: $scope.name
});

Where userid is some sort of unique id you shall know about, this could be an email or something, because you want to look up that person again, you need to use this key.

Firebase is just a key path store, so all locations in the three can be accessed as URLs

vonGohren
  • 929
  • 8
  • 22