[SOLVED] Returning value inside Moralis.Cloud.httpRequest function doesn't work

I need my cloud function to return a value from httpRequest, but returning inside Moralis.Cloud.httpRequest always returns undefined. I can log it to the console and it works, but when Iโ€™m trying to return, it shows as undefined:

Moralis.Cloud.define("getExternalData", async (request) => {
  	const logger = Moralis.Cloud.getLogger();
    	    
    Moralis.Cloud.httpRequest({
    method: 'GET',
    url: 'API URL',
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    body: {
      
    }
  }).then(function(httpResponse) {
      let dataObj = httpResponse.data;
      dataObj = JSON.stringify(dataObj);
      dataObj = JSON.parse(dataObj);
      
      
      const strength = parseInt(dataObj['attributes'][0]['value']);
      const constitution = parseInt(dataObj['attributes'][1]['value']);
      const dexterity = parseInt(dataObj['attributes'][2]['value']);
      const intelligence = parseInt(dataObj['attributes'][3]['value']);
      const charisma = parseInt(dataObj['attributes'][4]['value']);
      const luck = parseInt(dataObj['attributes'][5]['value']);
      const name = dataObj['name'];
      const image = dataObj['image'];
      
      const stats = {
       	strength: strength,
        constitution: constitution,
        dexterity: dexterity,
        intelligence: intelligence,
        charisma: charisma,
        luck: luck,
        name: name,
        image: image,
      };
      
      statsObject = JSON.stringify(stats);
      
      return statsObject;
      
  }, function(httpResponse) {
    logger.info('Request failed with response code ' + httpResponse.status);
  });

Even when Iโ€™m trying to just return true (for test purposes) it shows up as undefined. Returning outside the httpRequest works fine, but I need to return a value that Iโ€™m getting inside.

maybe you need to put return before Moralis.Cloud.httpRequest as in: NFTOwners beforesave function failing after update

1 Like

Yes, thatโ€™s it. Thanks :slight_smile:

1 Like