簡體   English   中英

在redux thunk中正確的鏈接操作方式?

[英]Proper way of chaining operations in redux thunk?

我第一次使用redux thunk 鏈接操作的正確方法是什么?

我想在給出用戶輸入后獲取位置,並且當Google Maps API數據響應時,我想立即使用該數據來獲取該位置的天氣。 Redux thunk正在運行,但僅適用於首次操作(獲取位置)。 request2 Data2始終是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 });
          });
        });
      };
    }

第二個請求很可能沒有返回一個名為response.data2的字段,因此當您對其進行data2時, data2將是未定義的。 您可能仍需要查找一個名為data的字段,但為它指定一個不同的本地參數名稱,例如: request2.then({data : data2})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM