Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140131

I am getting Unhandled Rejection (TypeError): Cannot read property 'temp' of undefined when trying to build a react app using openweatherapi

$
0
0

I am trying to build a weather app usind the open weather api https://openweathermap.org/guide and i am getting this error when clicking the get weather button. This is my main app.js renamed to Profile.js

import React, { Component } from 'react'
import jwt_decode from 'jwt-decode'
import Heading from './heading'
import Form from './form'
import Forecast from './forcast';

const api_key="secretnumber";

class Profile extends React.Component {



 componentDidMount() {
 const token = localStorage.usertoken
 const decoded = jwt_decode(token)
 this.setState({
  first_name: decoded.first_name,
  last_name: decoded.last_name,
  email: decoded.email
})
}
state={
temperature : "",
city : "",
country:"",
humidity:"",
pressure:"",
icon:"",
description:"",
error:""
}
getWeather = async (e) => {
 const city = e.target.elements.city.value;
 const country = e.target.elements.country.value;
 e.preventDefault();
 const api_call = await fetch('http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&units=metric&appid=${api_key}');

 const response = await api_call.json();
 if(city && country){
     this.setstate({
         temperature: response.main.temp,
         city: response.name,
         country: response.sys.country,
         humidity: response.main.humidity,
         pressure: response.weather[0].icon,
         description:response.weather[0].description,
         error:""
     })

 }else{
     this.setState({
         error: "Please fill out input fields..."
     })
 }
}
 render() {
return (
  <div className="container">
    <div className="jumbotron mt-5">
      <div className="col-sm-8 mx-auto">
        <h1 className="text-center">PROFILE</h1>
      </div>
      <table className="table col-md-6 mx-auto">
        <tbody>
          <tr>
            <td>Fist Name</td>
            <td>{this.state.first_name}</td>
          </tr>
          <tr>
            <td>Last Name</td>
            <td>{this.state.last_name}</td>
          </tr>
          <tr>
            <td>Email</td>
            <td>{this.state.email}</td>
          </tr>
        </tbody>
      </table>
      <div>
          <Heading/>
          <Form loadWeather={this.getWeather}/>
          <Forecast 
          temperature={this.state.temperature}
          city={this.state.city}
          country={this.state.country}
          humidity={this.state.humidity}
          pressure={this.state.pressure}
          icon={this.state.icon}
          description={this.state.description}
          error={this.state.error}/>
      </div>
    </div>

  </div>
 )
 }
 }

export default Profile

This is forcast.js

import React from 'react';

const Forecast = (props) =>{
return(
    <div>
        {props.country && props.city && <p>Location:{props.city},{props.country}</p>}
        {props.temperature && <p>Temperature: {props.temperature}</p>}
        {props.humidity && <p>Humidity:{props.humidity}</p>}
        {props.pressure && <p>Pressure: {props.pressure}</p>}
        {props.icon && <img src="" alt="weather icon"/>}
        {props.description && <p>Conditions:{props.description}</p>}
        {props.error && <p>{props.error}</p>}
    </div>
)
}
export default Forecast;

And this is my form.js

import React from 'react';

const Form = (props) =>{
return(
    <form onSubmit = {props.loadWeather}>
        <input type="text" name="city" placeholder="Choose a City" value="London"/>
        <input type="text" name="country" placeholder="Choose a Country" value="uk"/>
        <button>Get Weather</button>
    </form>
)
}
export default Form;

I am trying with city London and country uk and I am getting the error mentioned above. Thank you for your help. Really appreciate if this problem can be solved.


Viewing all articles
Browse latest Browse all 140131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>