无法访问Javascript中的对象/数组

无法访问Javascript中的对象/数组,javascript,reactjs,asynchronous,Javascript,Reactjs,Asynchronous,我可以查看console.log并查看我创建的数组,但一旦我尝试访问它,就会得到未定义的数组 async componentDidMount() { // fetch goal data for display let response = await fetchWithToken("http://localhost:8080/api/getGoals"); let goalData = await response.json(); goalData = aw

我可以查看console.log并查看我创建的数组,但一旦我尝试访问它,就会得到未定义的数组

  async componentDidMount() {
    // fetch goal data for display
    let response = await fetchWithToken("http://localhost:8080/api/getGoals");
    let goalData = await response.json();
    goalData = await goalData.filter(skill => skill.Skill === "CS_en");
    // get info from people API with distinct list rather than every row
    let people = new Set([]);
    goalData
      .filter(element => element.UpdatedBy !== null)
      .forEach(element => {
        people.add(element.UpdatedBy);
      });
    people = Array.from(people);
    // call peopleAPI
    const peopleObj = await peopleAPI(people);

    console.log("peopleObj :", peopleObj);
    console.log("peopleObj[0] :", peopleObj[0]);
    }
这是peopleAPI,我在这里调用另一个api并获取用户信息列表

const peopleAPI = people => {
  return new Promise(function(resolve, reject) {
    // get people API info
    const peopleObj = [];
    const apiPromises = [];
    if (people) {
      people.forEach(empid => {
        const apiPromise = fetch(
          `https://someApiCall/${empid}`
        )
          .then(res => res.json())
          .then(res => {
            peopleObj.push({
              empid: res.id,
              name: res.name.preferred ? res.name.preferred : res.name.full
            });
          })
          .then(() => apiPromises.push(apiPromise));
      });
      // once all promises have been resolved, return a promise with the peopleObj
      Promise.all(apiPromises).then(() => {
        resolve(peopleObj);
      });
    }
  });
};

export default peopleAPI;
console.log的结果


不要在
获取中使用push。然后
,只需返回其值,然后将其推送到api`

const peopleAPI = people => {`
  return new Promise(function(resolve, reject) {
    // get people API info
    const apiPromises = [];
    if (people) {
      people.forEach(empid => {
        const apiPromise = fetch(`https://someApiCall/${empid}`)
          .then(res => res.json())
          .then(res => {
            return {
              empid: res.id,
              name: res.name.preferred ? res.name.preferred : res.name.full
            }
          });
        apiPromises.push(apiPromise)
      });
      Promise.all(apiPromises).then((data) => {
        resolve(data);
      });
    }
  });
};

export default peopleAPI;
甚至更简单易读

const peopleAPI = people => {`
  const apiPromises = people.map(empid => {
    return fetch(`https://someApiCall/${empid}`)
      .then(res => res.json())
      .then(res => ({
        empid: res.id,
        name: res.name.preferred ? res.name.preferred : res.name.full
     }));
    });
  return Promise.all(apiPromises)
};

在第一个console.log中,您能帮我一个忙,改为记录
peopleObj.length
并查看调用此函数时的说明吗?peopleAPI看起来像什么?请向我们展示
peopleAPI
函数的实现,将
wait
放在
filter
call的结果上。请发布代码,而不是代码的图像。该函数可以大大优化。但仍然不确定代码为什么不等待结果。
Promise.all()
创建了一个
Promise
,不需要将其包装成一个。@ChrisG这是OP的尝试,更新为较短的版本感谢大家的帮助。我还不熟悉Javascript,并尝试更多地使用异步和承诺。