Can someone help me with this issue?
I'm facing an error with the JSON data retrieved from an API:
ERROR in src/app/weather/weather.component.ts(39,30): error TS2339: Property 'main' does not exist on type 'Iweather[]'
Here is the JSON data causing the problem:
{
"main": {
"temp": 28,
"feels_like": 32.95,
"temp_min": 28,
"temp_max": 28,
"pressure": 1008,
"humidity": 78
}
}
I am struggling to display the JSON data in my HTML file.
Below is my interface definition:
export interface IWeather {
name : string;
main: any[];
}
This is a snippet of my services.ts file:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, Subject } from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Iweather } from './Data-Interface';
@Injectable({
providedIn: 'root'
})
export class WeatherServiceService {
constructor( private http : HttpClient) { }
getRequest(val) : Observable<Iweather[]>{
let APP_ID ="myAPICode";
let cityName = val;
let url ='https://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&units=metric&appid=' + APP_ID;
return this.http.get<Iweather[]>(url);
}
}
This is part of my component code:
import { Component, OnInit } from '@angular/core';
import { WeatherServiceService } from '../weather-service.service';
import { Iweather } from '../Data-Interface';
import { FormBuilder, FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-weather',
templateUrl: './weather.component.html',
styleUrls: ['./weather.component.css']
})
export class WeatherComponent implements OnInit {
options: FormGroup;
floatLabelControl = new FormControl('auto');
constructor(private WeatherService : WeatherServiceService , fb: FormBuilder) {
this.options = fb.group({
floatLabel: this.floatLabelControl
});
}
public weatherData : Iweather[] = [];
ngOnInit() {}
public cityName ="";
public Status = "true";
public humidity = "";
public pressure = "";
public wind_speed = "";
public weather = "";
public temp :string;
getWeatherReport(value) {
this.Status = 'false';
this.cityName =value;
this.WeatherService.getRequest(this.cityName)
.subscribe((data : Iweather[]) => {
this.temp = data.main.temp;
this.humidity = data.main.humidity;
this.pressure = data.main.pressure;
this.weatherData = data;
});
}
}
And here is an excerpt from my HTML file:
{{ weatherData.name }}