Check if a record came back-end null in Angular

Asked

Viewed 1,351 times

2

I have a modal where I have two text inputs: CPF and Senha. In the input of CPF have a ng-blur who makes a Requisition GET every time I change fields. If I type a CPF that exists in the bank he made the GET correctly, but would like to display some warning if you do not find the CPF in the back-end.

When I try to fetch 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’t serialize null Objects] with root cause java.lang.Nullpointerexception: You can’t serialize null Objects

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

Function that makes the GET and is passed to the 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);

    });     
};

PAGE ERROR: inserir a descrição da imagem aqui

Page:

<div class="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>

2 answers

2

Just you check the return of loginAPI.getBeneficiario(usuario.cpf);.

try to do it this way:

  $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);

    });     
};
  • Yes, but how do I verify that return?

  • You make Ajax calls with certain json return ?

  • Yes, but I’m just not sure how to check this return. I’ve tried using promisse but I don’t think I did it right either.

  • 2

    Place a Debugger to see what is returning, so it is easy to know how to validate whether it is null or not. If it is returning undefined or ""

  • I fixed the function by placing promisse. I will edit the question with the new function and the error

  • Okay, I’m glad I can help you.

  • I edited the question and put the error that is appearing on the console and modified the function.

  • I changed my answer to try to help you with the new problem. console.log("uhul" +data);

  • Return that: uhul[object Object]

  • But in the browser you can see the content of this object, if you can send to help it.

  • How can I view it, I can only see the result of the GET request

Show 6 more comments

2


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

I would use 2 ways from the server side.

1st found record, return a Request with Status 200 (Ok), found no record, returns a Request with status 412 (Pre-condition 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 handle server side.

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

       if(retorno.data !== null || retorno.data !== undefined){
          //Código aqui,
       }
    });
  • I’m stirring here and I did it this way: .error(function(response, status) {&#xA; if(status === 500){&#xA; console.log("Status 500")&#xA; }&#xA; });

  • Is that even valid? will not bring any problems in the future, this way I did when the CPF is invalid it displays the message on the console, IE, the check is working.

  • 1

    I always use the first way, when program Server and Client, so I leave the server performing all treatments and client, only controlling the returns @Techies

  • I understood, taking advantage of the question how to fall into the Else put a red outline the CPF input?

  • if you are using Boostrap, you can add the has-error class, and have N ways to add this class dynamically ..

  • has some component of Angular that does this?

  • Not that I know of .. I use html input, boostrap and to dynamically change angular ng-class classes

  • ah n forget, if the answer solved your problem, to mark as solved ? @Techies.

  • Yes you did, now all that’s missing is the alert part, in case you don’t make it I’ll open another question.

Show 4 more comments

Browser other questions tagged

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