"Cannot read Property" after an http.get request

Asked

Viewed 417 times

4

Hello, all right?

I have the following problem. I am making a Restful request (http.get) and am writing the received object into a variable within $Scope. However, when I try to access this variable inside the controller I get the following error:

My controller:

.controller('chartEvapo', function($http, $scope) {
  $http.get(meurepositorio)
    .success(function(response) {
      angular.forEach(response, function(response) {
        $scope.cityOBJ = response;
      });
    });

    var teste = $scope.cityOBJ.info.etppri;
})

And the mistake:

Cannot read Property 'info' of Undefined

What I find strange is that I can print the figures as follows:

<div>
  {{cityOBJ.info.etppri}}
</div>

The console return.log(Response) is:

 Object{_id: Object, id:"1", info:Object"}
 _id: Object
 id: "1"
 info: Object
 __proto__: Object

EDIT:

With the following controller I had the following error:

.controller('chartEvapo', function($http, $scope) {
var teste;

$http
    .get(meurepositorio)
    .success(function(response) {
        $scope.cityOBJ = response;
        atualizaTeste();
    });

// está função ira atualizar a variável teste para você
function atualizaTeste() {
    this.teste = $scope.cityOBJ.info.etppri;
}
});

Typeerror: Cannot read Property 'etppri' of Undefined at actualizTeste (controllers.js)

EDIT 2: With JSON.stringify;

  function atualizaTeste() {
    console.log(JSON.stringify($scope.cityOBJ));
    this.teste = $scope.cityOBJ.info.etppri;
}

Console return:

[{"_id":{"$oid":"5807a914dcba0f490c71818b"},"id":"1","info":{"etppri":[10,112,10,112,10,14,10,112,10,155,142,50],"etppen":["10","11","12","132","3","2","80","60","55","22","112","15"],"etptho":["10","11","12","132","3","2","80","60","55","22","112","15"],"bh":{"ex":["30","30","30","30","30","20","80","30","35","22","30","35"],"pr":["30","30","30","30","30","20","80","30","35","22","30","35"]}}}]

  • 1

    Do not put "Solved" in the question title. The mechanism to mark as useful already has this purpose. Read more here

2 answers

3


Renan, use your controller this way to see if it will solve the problem.

.controller('chartEvapo', function($http, $scope) {
    var teste;

    $http
        .get(meurepositorio)
        .success(function(response) {
            $scope.cityOBJ = response;
            atualizaTeste();
        });

    // está função ira atualizar a variável teste para você
    function atualizaTeste() {
        this.teste = $scope.cityOBJ[0].info.etppri;
    }
});

I’m thinking that maybe the mistake happens because of $http and angular.forEach be asynchronous

  • I had the following error: "Typeerror: Cannot read Property 'etppri' of Undefined at updateThis"

  • @Renannunessteinck right, the mistake happened because info is undefined. try giving a console.log(JSON.stringify($scope.cityOBJ)) before the this.teste = ... to see what the object cityOBJ owns property.

  • He returned this: [{"_id":{"$oid":"5807a914dcba0f490c71818b"},"id":"1","info":{"etppri":[10,112,10,112,10,14,10,112,10,155,142,50],"etppen":["10","11",""12","132","3","2","80","60","55","22","112","15"],"etptho":["10","11"12","132","3","2","80","80","55","22","112",",",","15"],"bh"::{"ex":["30","30","30","30","30","20","80","30","35","22","30","35"],"pr":["30","30","30","30","30","20","80","30","35","22","30","35"]}}}].

  • @Renannunessteinck change the body of the function atualizaTeste for this.teste = $scope.cityOBJ[0].info.etppri;. I will do an Edit on the answer. The object cityOBJ is actually a array, so we need to get index 0.

  • Great, for what I need it is already enough, however, if I want to take the whole object, without having to identify the vector indicene, it is possible?

  • @Renannunessteinck if I’m not mistaken, and the Onosendai can confirm me, the Restsponse already comes as array, If that’s not true, you’ll have to see in your API the return of JSON. So, assuming Response automatically turns the server response into a array you don’t have a choice, but you can use a for or you can just take success the response[0], woe to you $scope.cityOBJ will be a object beautiful!

Show 1 more comment

2

  • The error persists. When I do "console.log(Answer.data)

  • The error persists. When I do "console.log(Response.data);" appears "Undefined".

  • @Renannunesstenck Could include in your question the returned object? (console.log(response);)

  • I put the result of console.log, Besides (I don’t know if it helps), but the object I’m returning is just an object (not an array), I’m doing a get with the "id" (not _id) as the search key. The database server I’m using is mlab.

Browser other questions tagged

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