简体   繁体   English

如何在反应中使用数组获取数据?

[英]How can I fetch data with an array in react?

I am trying to get all data of the applicants in an array:我正在尝试以数组形式获取申请人的所有数据:

Here is following code that how I fetch an array:以下是我如何获取数组的代码:

const [applicants, setTeamApplications] = useState([]);

  const fetchApplicationData = async () => {
    const res = await axios.get(`/api/v1/invites/team/applications/${teamId}`);

 const items = res.data.map((item) =>
  await axios.get(`/api/v1/profiles/profile/${item.applicant_publicId}`)
);

    // Set state
    setTeamApplications(res.data);
    // Toggle loading state
    setLoadingState(false);
  }; 

after I console.log(applicants)在我console.log(applicants)之后

it gave me this:它给了我这个:

[{…}]
0:
applicant_publicId: "La1618912810062"
application_letter: "apply"
id: 3
response: "Waiting on review"

Now im trying to fetch data again with the applicant_publicId to get more data:现在我尝试使用applicant_publicId publicId 再次获取数据以获取更多数据:

/api/v1/profiles/profile/${index.applicant_publicId}

How can I do that?我怎样才能做到这一点?

You can't await inside the .map callback as it is synchronous, but you can map the response data to an array of requests for the profiles and use Promise.all and wait for them all to resolve to an array of profiles.您不能在.map回调中await ,因为它是同步的,但您可以 map 响应数据对配置文件的请求数组并使用Promise.all来解析所有配置文件。

const [applicants, setTeamApplications] = useState([]);

const fetchApplicationData = async () => {
  // Toggle loading state
  setLoadingState(true);

  try {
    const res = await axios.get(`/api/v1/invites/team/applications/${teamId}`);

    const profileReqs = res.data.map((item) =>
      axios.get(`/api/v1/profiles/profile/${item.applicant_publicId}`)
    );

    const items = await Promise.all(profileReqs);

    // Set state
    setTeamApplications(res.data);
    setProfiles(items);

  } catch (error) {
    // handler any error state?
  } finally {
    // Toggle loading state
    setLoadingState(false);
  }
}; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React 中,如何获取数据并在 states 数组中渲染单个 object 而无需映射整个事物? - In React How can I fetch data and render a single object in a states array without mapping the whole thing? 如何从对象数组中获取数据,并通过 API 端点将其传递到 .fetch() 使用 React 所需的对象值? - How can I fetch data from an array of objects, pass it through an API end point to .fetch() the needed object value using React? 如何在 React Native 中将数据提取到我的数据表中? - How Can I Fetch Data into My Datatable in React Native? 如何从 Firebase 获取数据到我的 React 应用程序中 - How can I fetch data from Firebase into my React App 我如何在反应中使用 fetch 提交表单数据和文件? - how can i submit form data and file using fetch in react? 如何在 React 中从 Websocket 获取数据? - How can I fetch data from a Websocket in React? 如何获取作为对象而非数组的 Firebase 数据(实时数据库)? - How can I fetch Firebase data (Realtime Database) that is an object, not an array? 如何使用 React 获取 cookie 数据? - How do I fetch cookie data with React? 如何获取结果并将结果传递给反应中的下一个获取? - How can I fetch and pass the result to the next fetch in react? 在 react-admin 中,如何在使用 fetch 获取数据时更新当前视图? - In react-admin, how can i update my current view when i using fetch to get data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM