Mohammad Elsayed
Mohammad Elsayed

Reputation: 2066

Ionic 3 works with syntax error, but can't build with this error

I am building a weather application with ionic 3, my problem is when the response comes back (and I know the names of its keys) when I call one of them WeatherStorm (the IDE I am using) says:

property 'current_observation' does not exist on type object"

but the application is working when I try it on 'localhost:8100/ionic-lab'.

Until now no problem, I took this and said that the problem is from the editor(cuz it is working and giving correct results) but when I was trying to build the app to generate apk the Windows-Command-Line and Git-Bash both complained about this error. The exact command causing me the trouble is: "ionic cordova build --release android".

Here is the call method:

getWeather(city, state) {
  return this.http.get(this.url + '/' + state + '/' + city + '.json')
    .map(res => res);
}

And here is the response (here comes the error at current_observation):

this.weatherProvider.getWeather(
        this.location.city,
        this.location.state
      ).subscribe(weather => {
        this.weather = weather.current_observation;
        console.log(this.weather);
      })

      }).catch(()=> {

      });

Any idea?

Thank you in advance.

Upvotes: 1

Views: 219

Answers (4)

Maizied Hasan Majumder
Maizied Hasan Majumder

Reputation: 1053

After solve this bug, Sometime it's not work in IDE/Editor. So restart your computer and ionic serve your project again and try this code.

this.weatherProvider.getWeather(this.location.city, this.location.state)
      .subscribe((weather) =>{
        this.weather = weather["current_observation"];
      });

Upvotes: 1

Sachin
Sachin

Reputation: 2765

Try this way , it will do the trick

this.weatherProvider.getWeather(
        this.location.city,
        this.location.state
      ).subscribe(weather => {
        this.weather = weather["current_observation"];
        console.log(this.weather);
      }).catch(()=> {

      });

Upvotes: 2

sabithpocker
sabithpocker

Reputation: 15566

You can specify the type of data in the Observable returned. The following function states that it will return an Observable having data of type Weather. So at the place where you are subscribing it will expect a value of type Weather and will check type accordingly.

getWeather(city, state): Observable<Weather> {
    return this.http.get(this.url + '/' + state + '/' + city + '.json')
      .map<Weather>(res => res);
}

// place Weather in its own file and import in places where you use it.
class Weather {
   current_observation: any;
   ...otherKeys
}

If you don't care about any of these, you can try marking the return type as any

getWeather(city, state): Observable<any> {
    return this.http.get(this.url + '/' + state + '/' + city + '.json')
      .map(res => res);
}

Also, make sure your project and your IDE are using the same version of Typescript.

Upvotes: 0

Amit Chigadani
Amit Chigadani

Reputation: 29715

You should also declare the type of weather variable so that you don't get an error.

this.weatherProvider.getWeather(
        this.location.city,
        this.location.state
      ).subscribe((weather : Weather) => {
        this.weather = weather.current_observation;
        console.log(this.weather);
      })

      }).catch(()=> {

      });

Upvotes: 1

Related Questions