Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
No se pueden obtener datos de la matriz de objetos | React.js - openweatherAPI

Obtengo datos de openweatherAPI usando el kit de herramientas react-redux. Puedo obtener el nombre pero cuando quiero obtener main.temp da un error

typeError: no se pueden leer las propiedades de undefined (leyendo 'temp')

 export const fetchDefault = createAsyncThunk('weather/getWeather', async (selectedCity) => { const res = await axios(`https://api.openweathermap.org/data/2.5/weather?q=${selectedCity}&appid=6d735f036884219e3a12eeb6f7bf6f93`) return res.data});

ExtraReductores

 [fetchDefault.fulfilled]: (state , action) => { state.item = action.payload; console.log(state.item) },

Funciona si return res.data.main pero esta vez no puedo obtener el nombre.

índice.js

 const getCity = useSelector((state) => state.weather.item); {getCity.main.temp} // Gives Error..

La matriz se ve así ...

 { "weather": [ { "id": 802, "main": "Clouds", "description": "scattered clouds", } ], "main": { "temp": 300.15, "pressure": 1007, "humidity": 74, "temp_min": 300.15, "temp_max": 300.15 }, "wind": { "speed": 3.6, "deg": 160 "id": 2172797, "name": "Cairns", "cod": 200 }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su reductor guarda action.payload en state.item , por lo que creo que useSelector debería devolver state.item.name para getCity (suponiendo que desea acceder al nombre de la ciudad con ese selector).

Alternativamente, puede crear un selector para state.item y acceder a todo a través de eso.

Por cierto, su reductor debería devolver un valor, por lo que suponiendo que el estado era un objeto inicialmente, debería verse así:

 [fetchDefault.fulfilled]: (state , action) => { return {...state, item: action.payload} },
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!