TypeError: Cannot set property 'description' of undefined when using angularjs $http get request -
i have controller info user , find perfect fruit him. if in json file there isn't description of fruit, wikipedia (wikimedia api).
the issue i'm not able attach promise description variable.
i appriciate it take look,
thanks
app.controller('fruitsctrl', ['$scope', '$http', 'preferences', function ($scope, $http, preferences) { $scope.preferences = preferences; //what kind of fruits preferences user have // local json files has info fruits $http.get("partials/fruits.json").then(function(response) { $scope.data = response.data; // question -> practice??? $scope.fruits = {}; // @ json file fruits correspond preferences (i = 0; < $scope.preferences.length; i++) { (l = 0; l < $scope.data.length; l++) { if($scope.data[l].fruitproperties.indexof($scope.preferences[i]) > -1){ // add fruit details fruits object $scope.fruits[l] = $scope.data[l]; // if description of fruit not in json file // have var - "wikiname" wikimedia api if ($scope.fruits[l].description === undefined){ var wiki = $scope.fruits[l].wikiname; // wikimedia can use $http , not $http.get $http({ url: $scope.url = "https://en.wikipedia.org/w/api.php?format=json&callback=json_callback&action=query&prop=extracts&exintro=true&titles="+wiki, method: 'jsonp' }).success(function(response) { for(var id in response.query.pages) { $scope.fruits[l].description = response.query.pages[id].extract; } }); } } } } }, function () { $scope.sites = [{action: "error"}] //add somthing in case of error }); }]);
i suggest putting functionality service or factory work inside of controller.
i suggest 2 part approach. use $templaterequest accessing json , if there no data perform call wiki using $http.
as undefined error, assume attempting assign object yes? if try instantiating object before assigning.
yourvarname.prop = {}; yourvarname.prop = response;
sorry, clicked entire object, not new property undefined. above not work.
have considered using callback function inside of success function?
//inside success callback(response, l); //end success function callback (response, l) { $scope.yourproperties[l] = response; }
by moving assignment out of success may around issue causing undefined.
Comments
Post a Comment