Home > Enterprise >  Why am i facing ReferenceError: URLSearchParams is not defined?
Why am i facing ReferenceError: URLSearchParams is not defined?

Time:12-02

I am trying to get time entries through ClickUp API using the google apps scripts.

function getTime()
{
const query = new URLSearchParams({
  start_date: '0',
  end_date: '0',
  assignee: '0',
  include_task_tags: 'true',
  include_location_names: 'true',
  space_id: '0',
  folder_id: '0',
  list_id: '0',
  task_id: '0',
  custom_task_ids: 'true',
  team_id: '123'
}).toString();

const teamId = '123';
const resp = fetch(
  `https://api.clickup.com/api/v2/team/${teamId}/time_entries?${query}`,
  {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'tk_49237802_NCN'
    }
  }
);

const data =  resp.text();
console.log(data);

}

However it keeps on showing following error:

ReferenceError: URLSearchParams is not defined

Any reference or help will be highly appreciated

CodePudding user response:

I think that your script is for Javascript. That is not Google Apps Script. By this, such an error like ReferenceError: URLSearchParams is not defined occurs. If you want to convert your Javascript to Google Apps Script, how about the following sample script?

Sample script:

Please copy and paste the following script to the script editor of Google Apps Script, and save the script. And, please run myFunction. By this, the script is run.

function myFunction() {
  // Ref: https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
  String.prototype.addQuery = function (obj) {
    return this   Object.keys(obj).reduce(function (p, e, i) {
      return p   (i == 0 ? "?" : "&")  
        (Array.isArray(obj[e]) ? obj[e].reduce(function (str, f, j) {
          return str   e   "="   encodeURIComponent(f)   (j != obj[e].length - 1 ? "&" : "")
        }, "") : e   "="   encodeURIComponent(obj[e]));
    }, "");
  }

  const query = {
    start_date: '0',
    end_date: '0',
    assignee: '0',
    include_task_tags: 'true',
    include_location_names: 'true',
    space_id: '0',
    folder_id: '0',
    list_id: '0',
    task_id: '0',
    custom_task_ids: 'true',
    team_id: '123'
  };

  const teamId = '123';
  const baseUrl = `https://api.clickup.com/api/v2/team/${teamId}/time_entries`;
  const url = baseUrl.addQuery(query);
  const resp = UrlFetchApp.fetch(url,
    {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        Authorization: 'tk_49237802_NCN'
      }
    }
  );
  const data = resp.getContentText();
  console.log(data);
}

Note:

  • This sample script suposes that your showing Javascript works fine as Javascript. Please be careful about this. The request of this sample script is the same with your showing Javascript. But, if an error occurs, please confirm Authorization: 'tk_49237802_NCN', and query, and your URL, again.

Reference:

CodePudding user response:

Tanaike's answer is the way to go. But since all the URL parameters in the question are text strings, and they are not used for anything besides getting the .toString(), perhaps it is worth pointing out that you can accomplish the same like this:

  const query = Object.entries({
    start_date: '0',
    end_date: '0',
    assignee: '0',
    include_task_tags: 'true',
    include_location_names: 'true',
    space_id: '0',
    folder_id: '0',
    list_id: '0',
    task_id: '0',
    custom_task_ids: 'true',
    team_id: '123',
  }).map(row => row.join('='))
    .join('&');
  • Related