[SOLVED] Moralis cloud function returning undefined

Moralis.Cloud.define("getNFT", async (request) => {
  
  const logger = Moralis.Cloud.getLogger();
  let NFTId = request.params.nftId;
  let hexId = parseInt(NFTId).toString(16);
  let paddedHex = ("0000000000000000000000000000000000000000000000000000000000000000" + hexId).slice(-64)  
  let url = 'https://khnfrd2ukfku.usemoralis.com/' + paddedHex + '.json'
  
  logger.info(url);

  return Moralis.Cloud.httpRequest({url: url})
  .then(function(httpResponse){
    logger.info("SUCCESS");
    logger.info("httpResponse:");
    logger.info(httpResponse.data);
    return httpResponse
  }, function(httpResponse){
     // error
    logger.info("FAILURE");
  	logger.info('Request failed with response code ' + httpResponse.status);
  })
})

Hey guys, hope you can help. Im trying to return the cloud function above but Im returning undefined. Is there anything that I need to be doing to get the json response back.

A couple notes:

  • I was following a tutorial online and the code seems to be the same.
  • The url looks correct, ive been copy/pasting it from the dashboard into the browser and it it directing me the the right place.
    -The url is expected to return a json object.
  • I dont seem to be seeing any error logs or obvious issues in the dashboard

Thank you in advance,

what do you see in the logs?

  1. 2022-01-21T03:51:10.361Z - undefined
  2. 2022-01-21T03:51:10.361Z - httpResponse:
  3. 2022-01-21T03:51:10.360Z - SUCCESS

these are the last 3 logs, its hitting success, but httpResponse is empty

you can find various examples on this forum that work fine, this is one of them: NFTOwners beforesave function failing after update

1 Like

Got it!, in case anyone else is reading this, the code above seems correct. The error was on the client side, where then response needs to be unwrapped properly. The response will be undefined in the dashboard logs but good in the browser console.

  fetch("{url}/{funcName}?_ApplicationId={appId}&nftId=" + id )
        .then(response => response.json())
        .then(response => console.log(response))
1 Like