简体   繁体   English

对如何使用 axios 正确处理 javascript 承诺有些困惑

[英]Having some confusion around how to properly handle javascript promises with axios

So I have two simple functions, the first function makes an api call and retrieves 100 category ids and stores them in an array.所以我有两个简单的函数,第一个函数进行 api 调用并检索 100 个类别 id 并将它们存储在一个数组中。 I use lodash to randomly pick 6 of these category ids.我使用 lodash 随机选择这些类别 ID 中的 6 个。 The second function is suppose to make use of these 6 unique category ids and use them in the query string for the next 6 api calls made in the second function.第二个函数假设使用这 6 个唯一的类别 ID,并在第二个函数中进行的下 6 个 api 调用的查询字符串中使用它们。

 async function getCategoryIds() { const res = await axios.get('http://jservice.io//api/categories?count=100'); for (let cat of res.data) { categories.push(cat.id) } var sampleCategories = _.sampleSize(categories, 6); console.log(sampleCategories); return sampleCategories; } getCategoryIds() .then(getCategory) async function getCategory(sampleCategories) { const res1 = await axios.get(`http://jservice.io/api/category?id=${sampleCategories[0]}`); const res2 = await axios.get(`http://jservice.io/api/category?id=${sampleCategories[1]}`); const res3 = await axios.get(`http://jservice.io/api/category?id=${sampleCategories[2]}`); const res4 = await axios.get(`http://jservice.io/api/category?id=${sampleCategories[3]}`); const res5 = await axios.get(`http://jservice.io/api/category?id=${sampleCategories[4]}`); const res6 = await axios.get(`http://jservice.io/api/category?id=${sampleCategories[5]}`); } getCategory();

However, no matter how I rework it I still cannot get this error to go away:但是,无论我如何返工,我仍然无法消除此错误:

Uncaught (in promise) TypeError: Cannot read property '0' of undefined

Could somebody steer my in the right direction?有人可以引导我朝着正确的方向前进吗?

Thank you.谢谢你。

if your backend is exactly sending response an exact array then you should如果您的后端正在发送响应一个确切的数组,那么您应该

dont forget to give args when ur calling getCategory function当你调用 getCategory 函数时不要忘记给出 args

then edit your getCategory function然后编辑您的 getCategory 函数

async function getCategory(sampleCategories) {
    let arr = []
    const res1 = await axios.get('any url you want')
    //repeat 6 times
    arr = [res1, res2, ...otherElems]

   return arr
}

with 'then' syntax使用“then”语法

getCategoryIds()
    .then(response => getCategory(response))

with async await syntax使用异步等待语法

const firstResponseArr = await getCategoryIds();
const secondResponseArr = await getCategory(firstResponseArr);

Your Mistake你的错误

Calling getCategory();调用getCategory(); with passing any argument.通过任何参数。 Clearly async function getCategory(sampleCategories) needs an argument - sampleCategories which you failed to pass.显然async function getCategory(sampleCategories)需要一个参数 - 你未能通过的sampleCategories

Error intepretation错误解读

Uncaught (in promise) TypeError: Cannot read property '0' of undefined # sampleCategories

Call getCategory() with an argument使用参数调用getCategory()

This has nothing to do with axios but with your careless mistake.这与 axios 无关,而是与您粗心的错误有关。 (Don't worry, it happens to even the best of us) (别担心,即使是我们中最好的人也会发生这种情况)

Alright so what I was struggling with was trying to figure out how to properly utilize async/await -- this seemed to work for me:好吧,我正在努力解决的是试图弄清楚如何正确利用 async/await——这似乎对我有用:

 let categories = [] var sampleCategories = [] async function getCategoryIds() { const res = await axios.get('http://jservice.io//api/categories?count=100'); for (let cat of res.data) { categories.push(cat.id) } var sampleCategories = _.sampleSize(categories, 6); return sampleCategories; } getCategoryIds() async function getCategory() { var y = await getCategoryIds(); const res1 = await axios.get(`http://jservice.io/api/category?id=${y[0]}`); const res2 = await axios.get(`http://jservice.io/api/category?id=${y[1]}`); const res3 = await axios.get(`http://jservice.io/api/category?id=${y[2]}`); const res4 = await axios.get(`http://jservice.io/api/category?id=${y[3]}`); const res5 = await axios.get(`http://jservice.io/api/category?id=${y[4]}`); const res6 = await axios.get(`http://jservice.io/api/category?id=${y[5]}`); let arr = [res1, res2, res3, res4, res5, res6] return arr } getCategory();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM