[英]Making multiple Axios get requests with an array of links?
I have a function that I want to return all of the upcoming assignments for a student.我有一个 function,我想为学生返回所有即将完成的作业。 I start by running another async function that returns me all of the course ID's the student is in for a term and year.
我首先运行另一个异步 function,它返回学生在一个学期和一年内的所有课程 ID。 I then fill an array with.get urls for each class.
然后我为每个 class 填充一个数组。
My issue is with understanding and using Axios.all()
as I want to perform multiple calls to each class, and then to sort through each assignment and only save the assignments that are due later than the students current date.我的问题是理解和使用
Axios.all()
,因为我想对每个 class 执行多次调用,然后对每个作业进行排序,只保存迟于学生当前日期的作业。 The issue I am facing is that axios.all([urls])
returns UnhandledPromiseRejectionWarning: Error: Request failed with status code 401
.我面临的问题是
axios.all([urls])
返回UnhandledPromiseRejectionWarning: Error: Request failed with status code 401
。 I cant figure out why I cannot return the proper data from each url call.我无法弄清楚为什么我不能从每个 url 调用中返回正确的数据。 I narrowed it down to it being an issue with how I am setting up my axios.all call.
我将其缩小为我如何设置 axios.all 调用的问题。 Can someone point me in the right direction?
有人可以指出我正确的方向吗?
//Get all upcoming assignments from each class for a user
async function getAssignments(){
//Make multiple requests to api
axios
.all([urls])
//Loop through each assignment for each class
.then(
axios.spread((...res) =>{
for (var i = 0; i < res.data.length; i++) {
//If the due date of the assignment is due later than the users current date, push to array
if (res.data[i]['due_at'] > date.toISOString()) {
assignments.push(res.data[i])
}
}
//Output all assignments
return assignments
})
)
}
Update: I forgot to pass a token to my header and after a bit more tweaking it will now grab a specified url from my array.更新:我忘记向我的 header 传递一个令牌,经过更多调整后,它现在将从我的数组中获取指定的 url。 But I need it to now grab every url in the array and make the calls concurrently.
但是我现在需要它来抓取数组中的每个 url 并同时进行调用。 Though I am a bit stumped, would I just need to make a for loop for the
.get
or something more specific?虽然我有点难过,但我是否只需要为
.get
或更具体的东西制作一个 for 循环? Can someone point me in the right direction?有人可以指出我正确的方向吗?
//urls is the array filled with links. Need for the axios calls to happen concurrently
axios.all([axios.get(
urls[0],
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)])
//Loop through each assignment in a specfied class in canvas
.then(
axios.spread((...res) =>{
for (var i = 0; i < res[0].data.length; i++) {
//If the due date of the assignment is due later than the users current date, push to array
if (res[0].data[i]['due_at'] > date.toISOString()) {
assignments.push(res[0].data[i])
}
}
console.log(assignments)
})
)
.catch((err) => console.log(err))
You can set the header when at first at axios instance level assuming you have created your axios instance you can simply,您可以在最初在 axios 实例级别设置 header 假设您已经创建了 axios 实例,您可以简单地,
const axios = require('axios');
axios.defaults.headers.common['Authorization'] = 'Bearer AUTH_TOKEN';
//Get the course ID for each class the user is assigned to
const courseID = await getCurrentCourses('Spring', '2021')
//Fill the array with urls to each course assignment page
for(let i = 0; i < courseID.length; i++){
urls.push(axios.get('https://example.com/api/v1/courses/' + allCourseID[i] + '/assignments'));
}
//Make multiple requests to api
axios.all(urls)
//Loop through each assignment for each class
.then({......});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.