0

I am a newbie react learner and here trying to make an api call to display list of weather details(for test case I am trying to get "app_max_temp") but I am getting error as "cannot read property of map undefined at SearchCity(SearchCity.js:29)" .I have tried that url in postman and works absolutely fine giving response data. I have my code below. I can't figure out what is wrong and how to solve it. Any help would be appreciated. Thanks in advance.

import React,{useState} from 'react';

export default function SearchCity(){

    const[query,setQuery] = useState('');
    const[weatherResponse,setWeatherResponse] = useState([]);

    const searchCity = async (e) =>{
        e.preventDefault();
        const api_key ="YourApiKey";
        const url = `https://api.weatherbit.io/v2.0/forecast/daily?city=${query}&key=${api_key}`;
        try{
            const res = await fetch(url);
            const data = await res.json();
            console.log(data.results);
            setWeatherResponse(data.results);
        }
        catch(error){
            console.error(error);
        }

    }

    return(
        <>
            <form className="form" onSubmit={searchCity}>
                <label className="cityTitle" htmlFor="query">City Name : </label>
                <input type="text" value={query} name="query" onChange={(e)=>setQuery(e.target.value)} />
                <button className="btnSearch" type="submit">Search</button>
            </form>  
                {weatherResponse.map(res=>(
                <p>{res.app_max_temp}</p>
                )
                 )}
        </>
    )
}
4
  • What do you see in console.log(data.results);? Commented Jun 13, 2020 at 8:13
  • It doesn't compile , throws that map undefined error Commented Jun 13, 2020 at 8:19
  • 1
    map undefined should be a runtime error, not a compile time error. Syntax errors would be the type of error to prevent compile or fail before running Commented Jun 13, 2020 at 8:22
  • @Milan - That's what you get after submitting the form because there is no results entry in data and since you're assigning undefined to your state the .map part throws an error. Commented Jun 13, 2020 at 8:25

1 Answer 1

1

Weatherbit 16 Day Forecast API Documentation
Weatherbit 16 Day Forecast Swagger UI

The API returns those values in property data, not result:

        console.log(data.data);
        setWeatherResponse(data.data);

Make sure you have a proper API key (don't post it on here of course).

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

5 Comments

Here is the documentation.
hey @Milan please consider marking it as a right answer if it solved your issue.
@Ebdulmomen66 how do you mean? I already marked it but it won't show until I have 15 reputations.
Wow was there really a restriction like that?
@user120242 yea, unfortunately there is. And it showed now. Thanks to stack overflow team.

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.