0

I'm playing around learning a bit of REST APIs. I'm going to omit the URL I'm using for privacy purposes. The gist is that I'm receiving a dictionary-type object. The third entry in the dictionary is an array of JSON-like objects. When I try to look at this data through an alert it says:

undefined is not an object

I don't know what I have to do to either typecast it into something usable or format it upon entry.

(function(){

var Info = "";

var exampleData = [{}];

var app = angular.module('transinfo', ["ui.bootstrap.modal","angularjs-dropdown-multiselect"]);
app.controller('PanelController', function($scope,$http){

$http.get("ThisURL gives data")
.then(function(response){
  //var info = JSON.parse(response);
  exampleData = response.data;
  console.log(response.data);
  console.log(exampleData);

}).catch(function(data){

});

$scope.example1model = [];
// $scope.example1data = [ {id: 1, label: "David"}, {id: 2, label: "Jhon"}, {id: 3, label: "Danny"} ];
$scope.example1data = exampleData;
alert(exampleData.payload[0]);


Info = "blah";
this.Info = Info;


this.getInfo = function(){
  $http.get("this url also gives data")
  .then(function(response){
    //var info = JSON.parse(response);
    this.Info=response.data;
    console.log(response.data);
    console.log(response.data["success"]);
    alert(response.data["payload"][0].CityID);
  }).catch(function(data){
    this.Info = "Error";
  });
};
this.changeInfo = function(){
  this.Info = "Changed";
  console.log("hey");
};

this.selectTab = function(setTab){
  this.tab = setTab;
//this.getInfo();

};
this.isSelected = function(checkTab){
  return this.tab === checkTab;
};



});


})();

Here's some sample data I'd receive:

{success: true, message: "", error_code: "", payload: [{CityID: 72,     DescriptionES: "Puerto Rico", DescriptionEN: "Puerto Rico"}, {CityID: 999, DescriptionES: "VACIO", DescriptionEN: "EMPTY"}]}
freginold
  • 3,946
  • 3
  • 13
  • 28
  • Set a breakpoint in the debugger and inspect the object. There is no good reason in 2017 to be `alert`ing the response from an HTTP request. Nor are we psychic, we can't see the runtime values of vars in your code. – Jared Smith Aug 21 '17 at 19:14
  • what makes you think `undefined is not an object` comes from the alert. Most importantly, *which* alert are you talking about? You have an obvious problem in the first one – Sebas Aug 21 '17 at 20:00
  • `exampleData` is `undefined` so you can't read `payload` from it, but that error occurs before it tries to resolve the call to `alert` (function arguments are evaluated before the function you are trying to pass them to). – Quentin Aug 21 '17 at 20:01
  • Ok I'm sorry I'll provide some clarification. When I first add the response.data and the exampleData into the console log (before I use the alert) I do have the data I want in them. It's when I try to use the values such as exampleData in the alert that the error occurs. I Believe it comes from the alert cause the error log says it comes from that line. I do believe however that this is indeed an asynchronous problem as I read in the suggestion that this is a duplicate question. i'll investigate further. – Alfredo Pomales Aug 21 '17 at 20:34

0 Answers0