Check if a record came null from the back end in the Angular

2

I have a modal where I have two inputs of type text: CPF and Senha . In the input of CPF I have a ng-blur that makes a Request GET every time I change the field. If I type a CPF that exists in the database, it made GET correctly, but I would like to display some alert if I do not find CPF in back-end .

When I try to find a CPF that does not exist, this is the error that happens on the server:

  

GRAVE: Servlet.service () for servlet [default] in context with path   [/ UnimedWS] threw exception [java.lang.NullPointerException: You can not   serialize null objects] with root cause   java.lang.NullPointerException: You can not serialize null objects

How can I treat this error as best I can and put an alert in a message or even a red outline in input of CPF ?

Function that makes GET e is passed to ng-blur :

  $scope.getBeneficiario = function(usuario){
    loginAPI.getBeneficiario(usuario.cpf).success(function(data){
        console.log("uhul" +data);
        return loginAPI.getBeneficiario(usuario.cpf);

    })
    .error(function(response, status) {
        console.log("Resposta: "+response +"Status: "+status);

    });     
};

ERROR on page:

Page:

<divclass="alinhar">
   <form name="usuarioForm">
      <input class="form-control" type="text" name="nome" placeholder="CPF" ng-model="usuario.cpf" ng-required="true" ng-blur="getBeneficiario(usuario)" />
      <input class="form-control" type="text" name="fone" placeholder="Senha" ng-model="usuario.senha" 
         ng-required="true"/>
   </form>
</div>
<div class="modal-footer">
   <button class="btn btn-primary btn-block" ng-click="adicionarUsuario(usuario)" ng-disabled="usuarioForm.$invalid">Salvar</button>
</div>
    
asked by anonymous 18.09.2015 / 13:36

2 answers

2

Firstly, you need to check how you are handling the "Find No Record" factor on the server side.

I would use 2 forms on the server side.

1st found record, returned a Request with Status 200 (Ok), did not find record, returns a Request with status 412 (Precondition failed).

on the Client side.

(Como não sei o que seu getBeneficiario faz).

    $http('url get benefeficiar  + cpf').then(function(retorno) { 

    if(retorno.status === 200) { // successo
       //aqui não será null

    }else{
        //aqui o retorno é o null, por que você está tratando do lado do server.
    }
} );

2nd case not to deal with server side.

    $http('url get benefeficiar  + cpf').then(function(retorno) { 

       if(retorno.data !== null || retorno.data !== undefined){
          //Código aqui,
       }
    });
    
18.09.2015 / 14:38
2

Just check the% return of%.

try doing the following:

  $scope.getBeneficiario = function(usuario){
    loginAPI.getBeneficiario(usuario.cpf).success(function(data){
        console.log("uhul" +data);
        var res = loginAPI.getBeneficiario(usuario.cpf);
        if (res != null)
          return res;
        else return "";

    })
    .error(function(response, status) {
        console.log("Resposta: "+response +"Status: "+status);

    });     
};
    
18.09.2015 / 13:42