2017-05-30 45 views
0

我第一次使用redux thunk。鏈接操作的正確方式是什麼?在redux thunk中鏈接操作的正確方法?

我想獲取用戶輸入後給出的位置,當有響應數據從Google Maps API,那麼我想立即使用該數據來獲取該位置的天氣。 Redux thunk正在工作,但僅用於第一次操作(取回位置)。 Data2request2總是undefined,你能告訴我這是爲什麼嗎?

export function fetchLocation(city) { 
     const urlGoogle = `https://maps.googleapis.com/maps/api/geocode/json?address=${city}&key=${API_KEY_GOOGLE}`; 
     const request = axios.get(urlGoogle); 

     return (dispatch) => { 
     request.then(({ data }) => { 
      dispatch({ type: FETCH_LOCATION, payload: data }); 

      const lat = data.results["0"].geometry.location.lat; 
      const lng = data.results["0"].geometry.location.lng; 
      const urlWunder = `https://api.wunderground.com/api/${API_KEY_WUNDERGROUND}/forecast10day/q/${lat},${lng}.json`; 

      console.log(urlWunder); // Link is ok, it works in browser 

      const request2 = axios.get(urlWunder); 
      request2.then(({ data2 }) => { 
      console.log('Data2', data2); // Getting undefined, why ? 
      dispatch({ type: FETCH_WEATHER, payload: data2 }); 
      }); 
     }); 
     }; 
    } 

回答

1

這可能是因爲第二個請求未返回姓名的人士稱,response.data2場,所以當你解構它,data2將是不確定的。您可能仍然需要尋找名爲data的字段,但爲其指定一個不同的本地參數名稱,如:request2.then({data : data2})

+0

非常感謝你@markerikson! –

相關問題