I am trying angular for the first time and I am pretty stuck.This is the JSON data that I am receiving from an API-
{
"coord": {
"lon": 72.85,
"lat": 19.01
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 301.828,
"pressure": 1021.02,
"humidity": 100,
"temp_min": 301.828,
"temp_max": 301.828,
"sea_level": 1022.28,
"grnd_level": 1021.02
},
"wind": {
"speed": 3.52,
"deg": 246.002
},
"clouds": {
"all": 80
},
"dt": 1442131167,
"id": 1275339,
"name": "Mumbai",
"cod": 200
}
This is my function in controller-
(
function () {
var app=angular.module("weather",[]);
var query="";
app.controller("search",function(){
query="mumbai";
});
app.controller("result",['$http','$scope',function($http,$scope){
var city=query;
$http({
url:'http://api.openweathermap.org/data/2.5/weather',
method:'GET',
params:{q:city}
}).then(function (response) {
console.log(response);
console.log(response.name);
},function (response){
//error message
});
}]);
}
)();
Following are the console messages-
So I am not able to understand that if console.log(response)
is showing Object
then why console.log(response.name)
is showing undefined instead of "Mumbai".
Suggest a possible solution.
EDIT - Solution found : Console.log(response.data.name); seen the docs
response.data