Home > database >  array.push seems to create new array
array.push seems to create new array

Time:03-02

I am grappling with the way my array.push() works while making paginated API requests in Node.JS.

My API gives 10 results per request by default. The API gives a 'next' URL in the link header if there are more records to be retrieved. So I am using parse-link-header NPM to get this new link and make subsequent requests in a do while loop.

My code makes all necessary requests of assignment submissions for a student, for each course they are enrolled in.

E.g., the student is enrolled in 3 courses. And there are:

  • 19 assignments in course #1
  • 12 assignments in course #2
  • 0 assignments in course #3

I should get 3x arrays with the number of items in the array === the number of assignments in that course. E.g.:

//course 1
['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s']
//course 2
['a','b','c','d','e','f','g','h','i','j','k','l']
//course 3
[]

But for each request that is made, it is dividing it into 10 records per array (until there is no 'next' page).

So instead the arrays look like this when they are returned (simplified for example):

//course 1
['a','b','c','d','e','f','g','h','i','j']
['k','l','m','n','o','p','q','r','s']
//course 2
['a','b','c','d','e','f','g','h','i','j']
['k','l']
//course 3
[]

I know I am just doing something silly but am presently unable to figure it out.

My code:

    async function getAssignments(courses, student){
      //array to hold student assignment submissions
     const subs = [];
      let req;
      let parsed;
        for (var i=0; i<courses.length; i  ){
          let url = `https://${auths.canvas.domain}/api/v1/courses/${courses[i].id}/students/submissions?student_ids[]=${student}&include[]=assignment`;
          do {
              req = await canvReq(url);
              subs.push(req.data);
              parsed = parse(req.headers.link);
              if (parsed.next){
                url = parsed.next.url;
              }
            } while (parsed.next);
      }
      return subs;
    }

return await getAssignments(courses, student.id);

Many thanks for any assistance you can provide.

CodePudding user response:

subs.push(req.data) should be subs.push(...req.data). Using the spread operator (...) will push the individual elements of req.data (rather than the req.data array as a whole) to subs.

  • Related