javascript - 拉取api时redux中未定义数据

标签 javascript reactjs ecmascript-6 redux es6-promise

我正在尝试使用 redux 拉取 api,但我无法理解它。我不断收到错误“错误数据未定义”。

我正在使用 create-react-app 添加了一些库,如 thunk、记录器和中间件。如果我忘记提及某些内容,请说出来,以便我更新问题

这是我的行动

import axios from "axios";

export function fetchWeatherData() {
return function (dispatch) {
    axios.get('http://api.openweathermap.org/data/2.5/forecast/weather?q=London&units=metric&lang=hr&APPID=example')
        .then((response) => {
            dispatch({type: "FETCH_WEATHER_DATA_FULFILLED",results: response.data})
        })
        .catch((err) => {
            dispatch({type: "FETCH_WEATHER_DATA_REJECTED", results: err})
        })
    }
}

这是 reducer

 export default function reducer(state={
    cityName: data,
    nameOfCity: null,
    weatherDescription: null,
    windSpeed: null,
    temperature: null,
    maxTemperature: null,
    minTemperature: null
}, action) {
switch (action.type){
    case "FETCH_WEATHER_DATA": {
        return {...state,
            fetching: true
        }
    }
    case "FETCH_WEATHER_REJECTED": {
        return {...state,
            fetching: false,
            error: action.results
        }
    }
    case "FETCH_WEATHER_DATA_FULFILLED": {
        return {...state,
            fetching: false,
            fetched: true,
            nameOfCity: data.city.name,
            weatherDescription: data.list[0].weather[0].description,
            windSpeed: data.list[2].wind.speed,
            temperature: data.list[0].main.temp,
            maxTemperature: data.list[0].main.temp_max,
            minTemperature: data.list[0].main.temp_min
        }
    }
}

return state;
}

这是容器

import {FormContainer} from './containers/FormContainer';
import WeatherInfo from './components/WeatherInfo';
import {connect} from "react-redux"
import {updateInfo} from './actions/weather-apiActions'
import {fetchWeatherData} from './actions/weather-apiActions'

@connect((store) => {
  return {
    cityName: store.cityName.cityName,
    nameOfCity:store.nameOfCity.nameOfCity,
    weatherDescription:store.weatherDescription.weatherDescription,
    windSpeed:store.windSpeed.windSpeed,
    temperature:store.temperature.temperature,
    maxTemperature:store.maxTemperature.maxTemperature,
    minTemperature:store.minTemperature.minTemperature,
  }
 })

 class App extends Component {

   componentWillMount(){
     this.props.dispatch(fetchWeatherData());
   }

  }

存储文件

import { applyMiddleware, createStore } from "redux"

import logger from "redux-logger"
import thunk from "redux-thunk"
import promise from "redux-promise-middleware"

import reducer from "./reducers"

const middleware = applyMiddleware(promise(), thunk, logger())

export default createStore(reducer, middleware)

合并文件

import { combineReducers } from "redux"

    import cityName from "./weather-apiReducers"
    import nameOfCity from "./weather-apiReducers"
    import weatherDescription from "./weather-apiReducers"
    import windSpeed from "./weather-apiReducers"
    import temperature from "./weather-apiReducers"
    import maxTemperature from "./weather-apiReducers"
    import minTemperature from "./weather-apiReducers"

    export default combineReducers({
        cityName,
        nameOfCity,
        weatherDescription,
        windSpeed,
        temperature,
        maxTemperature,
        minTemperature
    })

编辑:错误显示的行。这是 reducer

./src/reducers/weather-apiReducers.js 中的错误

6:15 错误“数据”未定义 no-undef 14:5 警告 预期有默认情况 default-case

✖ 2 个问题(1 个错误,1 个警告)

最佳答案

变量data未定义。您必须使用 action.results:

// ...
case "FETCH_WEATHER_DATA_FULFILLED": {
    return {...state,
        fetching: false,
        fetched: true,
        nameOfCity: action.results.city.name,
        weatherDescription: action.results.list[0].weather[0].description,
        windSpeed: action.results.list[2].wind.speed,
        temperature: action.results.list[0].main.temp,
        maxTemperature: action.results.list[0].main.temp_max,
        minTemperature: action.results.list[0].main.temp_min
    }
}
// ...

关于javascript - 拉取api时redux中未定义数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403902/

相关文章:

javascript - 子组件在 POST 请求后不重新渲染父组件

javascript - 我的应用程序仅显示检索到的数据的一项

javascript - 如何在Firestore中不断地将对象添加到嵌套集合中

javascript - 从本地脚本导入 ES6 模块

javascript - 升级到 typescript 2 后找不到 (string)、Object、Date、Json 的需要索引

javascript - 除了 bootstrap 之外,如何向上自定义下拉菜单

javascript - JSON.parse 不运行

javascript - index.js 中未定义路由器

javascript - React Native Firebase auth().signInWithPhoneNumber() 在发布 APK 中返回意外错误

javascript - 努力让 ES6 这个绑定(bind)工作