0

Well, i tried everything but i couldn't solve the problem. I have this code, and i "force" an error in my server, but my callback function of the delete function is not invoking my error callback function, and the most weird thing is that i have the same code, but in another controller (and service) and it works normally.

Here is my controller code.

function buscaMesas() {
    Mesas.query(
        function(mesas) {
            $scope.mesas = mesas;
        },
        function(erro) {
            console.log(erro);
            $scope.success = false;
            $scope.mensagem = {texto: 'Não foi possível obter a lista'};
        }
        );
    }
    buscaMesas();

    $scope.remove = function(mesa) {
        $scope.mensagem = {texto: ''};
        Mesas.delete({id: mesa._id},
        buscaMesas,
        // here is not called
        function(erro){
            console.log(erro);
            $scope.success = false;
            if (erro.code = 1) {
                $scope.mensagem = {texto: 'Não foi possível remover a mesa pois está vinculado com PEDIDOS!'};
            } else {
                $scope.mensagem = {texto: 'Não foi possível remover a mesa!'};
            }
        }
        );
    };

Here is my server side controller

controller.removeMesa = function(req, res) {
    var _id = sanitize(req.params.id);
    var erro = {};

    Pedido.find({mesa: _id}).exec()
    .then(
        function(pedidos) {
            if (pedidos.toString() == '') {
                Mesa.remove({"_id": _id}).exec()
                .then(
                    function() {
                        res.end();
                    },
                    function(erro) {
                        return console.error(erro);
                    }
                );
            } else {
                // here i send the error
                erro.code = 1;
                return res.status(500).json(erro);
            }
        },
        function(erro) {
            console.error(erro)
            res.status(500).json(erro);
        }
    );
};

And here is my Service

angular.module('tcc').factory('Mesa',
function($resource) {
    return $resource('/mesa/:id');
})
.factory('Mesas',
function($resource) {
    return $resource('/mesas/:id');
});

UPDATE:

After i restart my computer, it works, probably it was some bug of angular or mongoDB, thanks

0 Answers0