2

I am using a weather api in my angular app and this is an example response of the api.

I get my data via this function :

getTempByName(name) {
return this.http.get('https://api.weatherbit.io/v2.0/current?' + 'city=' + name + 
'&key=mykey');
}

and then assign it to an object via this code where weatherData is my Object :

weatherData: object;
this.fetch.getTempByName(this.name).subscribe(data => {
  if (data) {
    this.weatherData = data;
  } else {
    this.noty.notify('error', 'The city Name you entered is incorrect');
  }});
}

and then in my html I am using this code to show the data :

<div class="container" *ngIf="weatherData">
  <p class="country">{{weatherData.data[0].city_name}}</p>
  <p class="temp">{{weatherData.data[0].temp}}°</p>
</div>

Everything runs smoothly when I run the app on a local server but as soon as I build it via this command ng build --prod I have this error:

ERROR in src/app/weahter-info/weahter-info.component.html(8,22): Property 'data' does not exist on type 'object'. src/app/weahter-info/weahter-info.component.html(9,8): Property 'data' does not exist on type 'object'. src/app/weahter-info/weahter-info.component.html(10,31): Property 'data' does not exist on type 'object'. src/app/weahter-info/weahter-info.component.html(11,19): Property 'data' does not exist on type 'object'. src/app/weahter-info/weahter-info.component.html(14,73): Property 'data' does not exist on type 'object'. src/app/weahter-info/weahter-info.component.html(17,54): Property 'data' does not exist on type 'object'.

I tried to find a way to ignore the error via various methods but I couldn't, any help will be welcome.

0

1 Answer 1

4

weatherData: object; change it to weatherData: any;

values come from API we want to opt-out of type checking and let the values pass through compile-time checks. To do so, we label these with the any type or you can create class or interface to avoid it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.