How to check if data already exists in the bank before inserting another in angular?

Asked

Viewed 292 times

4

I have already made a modal that receives a name, it sends the controller in the function of inserting new contact. I want to check if this data is already registered in the bank so that it does not register again. Anyone knows how I can do it in Angularjs?

I’ve done it so far...

//Carrega as categorias existentes

 var carregaCategorias = function(){
            var tam = $scope.categoria.length;
            for (var i = 0; i < tam; i++)
                $scope.categorias[i] = $scope.categoria[i].nome;
        }

//Verifica se já está inserido

$scope.createCategory = function(typeOfCategory, modalId){ //OK

            var typeCategory;
            var category = $scope.novaCategoria;

            if(category == ?? )
  • When you check if it is already inserted, do you send the "new" object in the verification method? If yes, you could do a SELECT COUNT in the table with an equality/LIKE clause. SELECT COUNT(nome) FROM categoria WHERE nome LIKE %dado_procurado%, for example. And ai, if the result is zero, allow the addition.

  • Good morning! Friend or vc creates a query in the database as mentioned above and returns a value indicating whether or not there is a similar item or you make a request in the API bringing all the records and checks on the front if you already have a similar record, but I believe that a query doing this verification would be ideal.

1 answer

0

I believe that the best way would be to do this at the proper time to insert and through and only from your backend code do this check if it already exists, if there is no insert

Browser other questions tagged

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