javascript - 它作为组合值传递 4,5,6

标签 javascript html arrays reactjs redux

https://codesandbox.io/s/redux-async-actions-xjdo7

<FetchButton
          onFetchClick={() => {
            store.dispatch(dispatchFunc => {
              dispatchFunc({ type: "FETCH_DATA_START" });
              axios
                .get("https://reqres.in/api/users?page=2")
                // axios.get('https://jsonplaceholder.typicode.com/posts')
                .then(response => {
                  console.log("response.data.data---->", response.data.data);
                  console.log(
                    "response.data.data[0].id---->",
                    response.data.data[0].id
                  );
                  dispatchFunc({
                    type: "RECEIVED_DATA",
                    payload: response.data.data
                  });

                  let firstAPIid = response.data.data.map(obj => {
                    return obj.id;
                  });
                  console.log("firstAPIid---->", firstAPIid);

                  return new Promise((resolve, reject) => {
                    //var url = `https://jsonplaceholder.typicode.com/comments?postId=3`;
                    var url =
                      `https://jsonplaceholder.typicode.com/comments?postId=` +
                      firstAPIid;
                    //response.data.data[0].id;

                    console.log("second url---->", url);

                    axios
                      .get(url)
                      .then(response => {
                        var lFilterData = "";
                        //memberGroupingHelper.filterData(response.data, additionalParams);
                        resolve(lFilterData);
                      })
                      .catch(error => {
                        if (error.response) {
                          console.log(
                            `@@@@@@@@@@@@@@ service error from helpeeeeeer reject`
                          );
                        }
                        reject("");
                      });
                  });
                })
                .catch(err => {
                  dispatchFunc({ type: "FETCH_DATA_ERROR", payload: err });
                });
            });
          }}
        />

最佳答案

我发现了你的问题。它正在发生,因为您没有处理 promise 的结果。为此,只需添加 .then().catch() 函数:

<FetchButton
          onFetchClick={() => {
            store.dispatch(dispatchFunc => {
              dispatchFunc({ type: "FETCH_DATA_START" });
              axios
                .get("https://reqres.in/api/users?page=2")
                // axios.get('https://jsonplaceholder.typicode.com/posts')
                .then(response => {
                  console.log("response.data.data---->", response.data.data);
                  console.log(
                    "response.data.data[0].id---->",
                    response.data.data[0].id
                  );
                  dispatchFunc({
                    type: "RECEIVED_DATA",
                    payload: response.data.data
                  });

                  let firstAPIid = response.data.data.map(obj => {
                    return obj.id;
                  });
                  console.log("firstAPIid---->", firstAPIid);

                  return new Promise((resolve, reject) => {
                    //var url = `https://jsonplaceholder.typicode.com/comments?postId=3`;
                    var url =
                      `https://jsonplaceholder.typicode.com/comments?postId=` +
                      firstAPIid;
                    //response.data.data[0].id;

                    console.log("second url---->", url);

                    axios
                      .get(url)
                      .then(response => {
                        var lFilterData = "";
                        //memberGroupingHelper.filterData(response.data, additionalParams);
                        resolve(lFilterData);
                      })
                      .catch(error => {
                        if (error.response) {
                          console.log(
                            `@@@@@@@@@@@@@@ service error from helpeeeeeer reject`
                          );
                        }
                        reject("");
                      });
                  }).then((previousResponse) => {
                    //Here you resolved the promise with the resolve value above
                    console.log(previousResponse)
                  }).catch((error) => {
                    //Here you resolved the promise with the reject value above
                    console.log(error);
                  });
                })
                .catch(err => {
                  dispatchFunc({ type: "FETCH_DATA_ERROR", payload: err });
                });
            });
          }}
        />

我没有看到对 Promise 的任何使用,因为您想要实现的目标只需使用 axios 即可完成。

编辑: 只需使用 axios 即可。修改如下:

<FetchButton
        onFetchClick={() => {
            store.dispatch(dispatchFunc => {
              dispatchFunc({ type: "FETCH_DATA_START" });
              axios
                .get("https://reqres.in/api/users?page=2")
                // axios.get('https://jsonplaceholder.typicode.com/posts')
                .then(response => {
                  console.log("response.data.data---->", response.data.data);
                  console.log(
                    "response.data.data[0].id---->",
                    response.data.data[0].id
                  );
                  //First of all we'll create the number of requestes base on the previous Response
                  const promises = response.data.data.reduce((previousValue, { id }) => { 
                    previousValue.push(axios.get(`https://jsonplaceholder.typicode.com/comments?postId=${id}`));
                    return previousValue;
                  },[]);
                  //We use the built in function to fetch the data
                  axios.all(promises)
                    .then((responses) => {
                      //Here you have all responses processed
                      const emailsMapped = responses.reduce((previousValue, { data }) => {
                        const emails = data.map(({ email }) => email)
                        previousValue.push(...emails);
                        return previousValue;
                      }, [])
                      //You send the emails you want  
                      dispatchFunc({
                          type: "RECEIVED_DATA",
                          payload: emailsMapped
                        });
                      console.log(emailsMapped);
                    })
                })
                .catch(err => {
                  dispatchFunc({ type: "FETCH_DATA_ERROR", payload: err }); 
                });
            }); 
          }}
        />

还修改了 DataList 中没有 first_name

的这一行
listItems.push(<div key={fetchedDataId++}>{elem}</div>);

关于javascript - 它作为组合值传递 4,5,6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57100587/

相关文章:

javascript - meteor 角色 : Can't mix grouped and non-grouped roles

html - Angular 和 Node JS 中的路由问题 [angular]

javascript - 在ejs中设置选择选项值

javascript - 在 Javascript 中使用 map 方法迭代 dom 节点

php - 如何像mysql一样对数组进行排序

javascript - nodejs 流可读卡在第一个可读对象

javascript - 正则表达式字符限制

javascript - 单击写入文件时的 html/javascript

python - 如何在 Python 中垂直连接两个数组?

javascript - Twitter typeahead,KnockoutJS 不会在 Chrome/Explorer 中触发 onchange/update observable