0

i have a problem since i want to get multiple data from a single api call How can i return more than one value from it? I'm running this function, it create the correct logs, but not the return part.

function getParts(axieID) {
  const response = UrlFetchApp.fetch(`https://api.axie.technology/getaxies/${axieID}`);
  const data = JSON.parse(response.getContentText());

  try {
    Logger.log(data.class)
    Logger.log(data.parts[1].name)
    return {
      total: data.class
    }, {
      total1: data.parts[1].name
    };
  } catch (e) {
    Logger.log(e)
    return {
      total: 'error',
      total1: 'error'
    }
  }
}

EDIT

var {total} = getParts(axieID);
  if (total != 'error') {
  sh3.getRange('J'+counter).setValue(total);
  }
  Utilities.sleep(700)
  }
  var {total1} = getParts(axieID);
  if (total1 != 'error') {
  sh3.getRange('K'+counter).setValue(total1);
  }
  Utilities.sleep(700)

SOLUTION (total changed with classe, and total1 changed with orecchie):

function getParts(axieID) {
const response = UrlFetchApp.fetch(`https://api.axie.technology/getaxies/${axieID}`);
const data = JSON.parse(response.getContentText());

try {
  Logger.log(data.class)
  Logger.log(data.parts[1].name)
  return {classe : data.class, orecchie : data.parts[1].name};
}
catch (e) { 
           Logger.log(e)
           return {
               classe: 'error', orecchie: 'error'
           }
   }
}

  var {classe,orecchie} = getParts(axieID);
  if (classe != 'error') {
  sh3.getRange('J'+counter).setValue(classe);
  }
  if (orecchie != 'error') {
  sh3.getRange('K'+counter).setValue(orecchie);
  }
  Utilities.sleep(700)
  }
Lorenzo Pimps
  • 123
  • 1
  • 16
  • Just look at your own catch block to see how to do this... –  Feb 22 '22 at 11:11
  • As for your follow up question: `const { total, total1 } = getParts(axieID);` Although even if you don't know about this syntax, you could still do `const result = getParts(axieID);` then simply access `result.total` and `result.total1` –  Feb 22 '22 at 11:13

1 Answers1

1

Approach # 1:

Return an object which includes both properties like that and easily use them by calling getParts(id).total or getParts(id).total1 :

function getParts(axieID) {
const response = UrlFetchApp.fetch(`https://api.axie.technology/getaxies/${axieID}`);
const data = JSON.parse(response.getContentText());

try {
  Logger.log(data.class)
  Logger.log(data.parts[1].name)
  return {total : data.class, total1 : data.parts[1].name};
}
catch (e) { 
           Logger.log(e)
           return {
               total: 'error', total1: 'error'
           }
   }
}

Approach # 2: By returning array

function getParts(axieID) {
const response = UrlFetchApp.fetch(`https://api.axie.technology/getaxies/${axieID}`);
const data = JSON.parse(response.getContentText());

try {
  Logger.log(data.class)
  Logger.log(data.parts[1].name);
  let total=data.class;
  let total1=data.parts[1].name;
  return [total, total1];
}
catch (e) { 
           Logger.log(e)
           return {
               total: 'error', total1: 'error'
           }
   }
}
Asad Haroon
  • 476
  • 3
  • 9