Erase Logs in Firebase

1

I'm developing a small chat using Firebase . My intention was to make login in my application and save usuarioLogado to the database and to make logoff delete that user from the database.

Currently I can only save the user at the time of entering, at the time of deleting simply nothing happens, no error happens as well, the 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;

});

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 delete these records?

    
asked by anonymous 19.11.2015 / 11:21

1 answer

0

I solved the problem as follows:

Controller in the function that does logoff I made a foreach that traverses all records that are in 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 in 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;

});
    
19.11.2015 / 14:05