0

I am using google task API for inserting tasks in google tasks & it's also working properly when I am only adding one task at a time but the problem came when I am inserting multiple tasks in one go. I tried every resource that I know but still didn't get any.

I am using react.js as the front end.

Any help or sharing is most appreciated.

Docs that I used => https://developers.google.com/tasks/reference/rest/v1/tasklists/insert?apix_params=%7B%22resource%22%3A%7B%22title%22%3A%22T1%22%7D%7D

My code:-

function addManualTasks() {
  const tasks = [
    { title: 'Task 1', notes: 'This is the first task' },
    { title: 'Task 2', notes: 'This is the second task' },
  ];

  // Inserting in the array form
  // gapi.client.tasks.tasks.insert({ tasklist: 'MTc0MTY5NjA3MTc2ODY1MDAwNjk6MDow', resource:tasks}).then(res => console.log('res', res))

  // Inserting in the object of array form
  gapi.client.tasks.tasks
    .insert({
      tasklist: 'MTc0MTY5NjA3MTc2ODY1MDAwNjk6MDow',
      resource: { items: tasks },
    })
    .then((res) => console.log('res', res));
}

Error that I am getting:- error

Ritik
  • 51
  • 7

1 Answers1

0

One thing to keep in mind is that the Google Tasks API only allows you to insert up to 100 tasks at a time. 1.Use a for loop

function addManualTasks() {
  const tasks = [
    { "title": "Task 1", "notes": "This is the first task" },
    { "title": "Task 2", "notes": "This is the second task" }
  ];

  for (let i = 0; i < tasks.length; i++) {
    gapi.client.tasks.tasks.insert({ tasklist: 'MTc0MTY5NjA3MTc2ODY1MDAwNjk6MDow', resource: tasks[i] }).then(res => console.log('res', res));
  }
}

2.Use Promise.all

function addManualTasks() {
  const tasks = [
    { "title": "Task 1", "notes": "This is the first task" },
    { "title": "Task 2", "notes": "This is the second task" }
  ];

  const promises = tasks.map(task => {
    return gapi.client.tasks.tasks.insert({ tasklist: 'MTc0MTY5NjA3MTc2ODY1MDAwNjk6MDow', resource: task });
  });

  Promise.all(promises).then(responses => console.log('responses', responses));
}
  • Thanks, Himanshu for giving the suggestion but I know this already, I want to directly pass the array of tasks not by iterations. – Ritik Mar 18 '23 at 22:07