【问题标题】:Dealing with error when making fetch call in redux在 redux 中进行 fetch 调用时处理错误
【发布时间】:2016-09-14 19:53:18
【问题描述】:

我正在构建一个天气应用程序,用于返回特定城市的天气详细信息。输入有效位置后,一切正常。当我在搜索字段中输入垃圾字符串(例如“afasdfldsakfh”)并按 Enter 时,就会出现我的问题。这是有道理的,因为 api 调用不应该找到该位置。

我在控制台中收到以下错误:

Uncaught TypeError: Cannot read property 'observation_location' of undefined

这来自 RECEIVE_WEATHER 情况下的减速器。当有效负载无效并且缺少为我的状态设置道具所需的信息时,我不确定我需要做什么。任何帮助将不胜感激,谢谢!

export function receiveWeather(json) {
  console.log(json);
  return {
    type: RECEIVE_WEATHER,
    payload: json
  };
};

export function fetchingWeather(location) {
  const init = {
    method: 'GET'
  };
  return dispatch => {
    dispatch(requestWeather())
    return fetch(weatherUrl + location + '.json', init)
      .then(response => response.json())
      .then(json => dispatch(receiveWeather(json)))
  };  
};

export function weather(state = {
  isFetching: false,
  city: '',
  country: '',
  weather_image: '',
  weather_description: '',
  weather_number: '',
  wind_dir: '',
  wind_speed: '',
  humidity: '',
  precipitation: ''
}, action) {
  switch (action.type) {
    case REQUEST_WEATHER:
      return Object.assign({}, state, {
        isFetching: true
      });
    case RECEIVE_WEATHER:
      return Object.assign({}, state, {
        isFetching: false,
        city: (action.payload.current_observation.observation_location.full),
        country: (action.payload.current_observation.observation_location.country),
        weather_image: (action.payload.current_observation.icon_url),
        weather_description: (action.payload.current_observation.weather),
        weather_number: (action.payload.current_observation.temp_c),
        wind_dir: (action.payload.current_observation.wind_dir),
        wind_speed: (action.payload.current_observation.wind_kph),
        humidity: (action.payload.current_observation.relative_humidity),
        precipitation: (action.payload.current_observation.precip_today_in)
      });
    default:
      return state
  };
};

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:

    在您的 fetch 调用中使用附加错误处理程序,如下所示:

    return fetch(weatherUrl + location + '.json', init)
      .then(response => response.json())
      .then(json => dispatch(receiveWeather(json)))
      .catch(err => handleError(err))
    

    我不知道您正在使用fetch 的什么实现,但API 上通常有catcherror 方法。

    您还可以添加一个这样处理错误的案例:

    case RECEIVE_WEATHER_ERROR: 
       return ...
    

    在这种情况下,您也可以在错误处理程序中分派错误

    return fetch(weatherUrl + location + '.json', init)
      .then(response => response.json())
      .then(json => dispatch(receiveWeather(json)))
      .catch(err => dispatch(handleError(err)))
    

    handleError 可以像其他函数一样实现:

    function handleError(err) {
      return {
        type: RECEIVE_WEATHER_ERROR,
        payload: err
      }
    }
    

    如果您没有收到错误,而是一个空的 json 对象,其中 payload.current_observation 未定义

    在您的案例陈述中添加以下内容:

    case RECEIVE_WEATHER:
      if (!action.payload.current_observation) return state
      ...
    

    【讨论】:

    • 感谢 JoshSGman 的回复。我在 package.json 文件中没有看到我的 fetch 实现。但是,我确实在我的 node_modules 文件夹中看到了“whatwg-fetch”。我正在使用这个反应骨架:github.com/joellongie/dustDevil。添加 '.catch(err...'
    • @Banner 尝试使用.error(err => dispatch(handleError(err)) 而不是 .catch
    • 在控制台中得到这个:Uncaught TypeError: fetch(...).then(...).then(...).error is not a function
    • @Banner 我查看了whatwg-fetch 模块,看起来.catch 是要走的路。你把你的console.log放在哪里?
    • .catch 现在返回原始错误。 console.log 位于 handleError 函数中。我把它放在那里,看看我能不能到达那里。感谢您迄今为止的所有帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多