Delete records in Firebase

Asked

Viewed 1,425 times

1

I am developing a small chat using the Firebase. My intention was to do the login in my application and save the usuarioLogado in the database and by logoff delete this user from the database.

Currently I can only save the user at login time, at delete time simply nothing happens, no error happens too, user just is not deleted.

Factory:

angular.module("UnionTi").factory('User', function($firebaseArray,$firebaseObject) {
  var ref = new Firebase(URL);
  var users = $firebaseArray(ref.child('users'));

  var User = {
    all: users,
    create: function (user) {
      return users.$add(user);
    },
    get: function (userId) {
      return $firebaseObject(ref.child('users').child(userId));
    },
    delete: function (user) {
      console.log("usuario: "+user.codigo)
      return users.$remove(user);
    }
  };

  return User;

});

In the controller where the logoff function is located:

angular.module("UnionTi").controller("homeCtrl", function($scope, $state, $location, userAuthAPI, User){

  var init = function(){
    $scope.usuarioLogado = userAuthAPI.getUserAuth();
    /**Salva o usuario no firebase*/
    User.create($scope.usuarioLogado);
  }

  $scope.sair = function(){
    /**Recupera o usuario logado*/
    $scope.usuarioLogado = userAuthAPI.getUserAuth();
    /**Deleta o usuário do firebase*/
    User.delete($scope.usuarioLogado)
    /**Faz o logout*/
    userAuthAPI.logout();
    $state.go('login', {}, {reload: true});
  }

  init();
})

How do I erase these records?

1 answer

0


I solved the problem as follows:

In the Controller in the function that makes the logoff I made a foreach that goes through all the records that are on Collection of Firebase:

angular.module("UnionTi").controller("homeCtrl", function($scope, $state, $location, userAuthAPI, User){

  $scope.users = User.all;

  var init = function(){
    $scope.usuarioLogado = userAuthAPI.getUserAuth();
    /**Salva o usuario no firebase*/
    User.create($scope.usuarioLogado);
  }

  $scope.sair = function(){
    /**Recupera o usuario logado*/
    $scope.usuarioLogado = userAuthAPI.getUserAuth();
    /**Percorre a lista de usuários*/
    angular.forEach($scope.users, function(userBD) {
      /**Se o codigo do usuarioLogado for igual ao do usuário que está no Firebase então podemos deleta-lo*/
      if($scope.usuarioLogado.codigo == userBD.codigo){
        console.log("são iguais!")
        User.delete(userBD);
      }
    })
    /**Faz o logout*/
    userAuthAPI.logout();
    $state.go('login', {}, {reload: true});
  }


  init();
})

And at the Factory:

angular.module("UnionTi").factory('User', function($firebaseArray,$firebaseObject) {
  var ref = new Firebase('https://npio.firebaseio.com');
  var users = $firebaseArray(ref.child('users'));

  var User = {
    //Lista todos os usuários
    all: users,
    create: function (user) {
      return users.$add(user).then(function(ref){
        var id = ref.key();
        console.log("added record with id " + id);
        users.$indexFor(id); // returns location in the array
      });
    },
    get: function (userId) {
      return $firebaseObject(ref.child('users').child(userId));
    },

    delete: function (user) {
      console.log("USER: "+user.$id)
      users.$remove(user).then(function(ref) {
        ref.key() === user.$id; // true
      });
    }
  };

  return User;

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.