gpt4 book ai didi

javascript - 值(value)迟到? “Value below was evaluated just now”

转载 作者:行者123 更新时间:2023-11-30 20:15:02 26 4
gpt4 key购买 nike

let currComp = this;
let projects = []
let dataArr = []


async function getData() {
let getProject =
await axios.get('url', {
auth: {
username: 'username',
password: 'pw'
}
})

let projects = await getProject.data.value;
let arr = []
let KPIs = []

projects.map(project => {
let item = () => axios.get(`url`, {
auth: {
username: 'username',
password: 'pw'
}
})

arr.push(item)
console.log('arr', arr)
})

let results = await axios.all(arr)

results.map(result => {

result().then(function(v) {
KPIs.push(v.data.value)
})
})

}

getData();

我想做的是:

  1. 调用 axios 来获取项目的名称。

  2. 使用这些获取的名称来调用多个 axios 调用。这应该会给我该项目的一些数据。

  3. results = await axios.all(arr) 包含为我提供对 API 调用的响应的函数。

  4. 响应被推送到数组 KPIs 中。

  5. 当我在最后尝试 console.log('KPIs', KPIs) 时,它给了我

enter image description here

这似乎是一个空数组,但是当我打开它时,

enter image description here

它实际上具有我想要的值。

问题是,当我尝试在我的代码中使用这个数组时,它只给我数组的第一项。

我试着在网上搜索这个问题,它只告诉我值(value)迟到了。

我想使用带有完整结果的完整数组。

我该如何解决?

最佳答案

results = await axios.all(arr) contains functions that gives me the responses to the API call.

是的,但这毫无意义。您不希望返回响应 promise 的函数,并且在函数数组上调用 axios.all 或 await 是没有用的。

相反,您需要构建一个 promise 数组,all()await that

您也不需要使用 then。您可以大大简化代码:

async function getData() {
const getProject = await axios.get('url', {
auth: {
username: 'username',
password: 'pw'
}
});

const projects = getProject.data.value;
const promises = projects.map(project =>
axios.get(`url`, {
auth: {
username: 'username',
password: 'pw'
}
})
);

const results = await axios.all(promises)

const KPIs = results.map(v => v.data.value);
}

关于javascript - 值(value)迟到? “Value below was evaluated just now”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52007379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com