Call API within Cloud Function

Hi,

I’ve been trying to find a solution to this and I saw there was a similar question to this already but looking at it did not solve my issue. I am not sure what I am doing wrong here as it looks like it should work. Any suggestions? Thanks in advance!

Moralis.Cloud.define("getIt", async (request) => {
    const results = ""
 let URL= 'https://api.coingecko.com/api/v3/coins/velas?localization=false&tickers=false&community_data=false&developer_data=false&sparkline=false'
 
  Moralis.Cloud.httpRequest({
  url: URL,
    followRedirect:true
}).then(function(httpResponse) {
  console.log(httpResponse.text);
    results = JSON.parse(httpResponse.text)
    
    return results
    
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});
  });

Hi @mkrow,

Make sure to use the return keyword after the HTTP request.

Let me know if this work. Thanks.

Moralis.Cloud.define("getIt", async (request) => {
    const results = ""
 let URL= 'https://api.coingecko.com/api/v3/coins/velas?localization=false&tickers=false&community_data=false&developer_data=false&sparkline=false'
 
  return Moralis.Cloud.httpRequest({
  url: URL,
    followRedirect:true
}).then(function(httpResponse) {
  console.log(httpResponse.text);
    results = JSON.parse(httpResponse.text)
    
    return results
    
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});
  });

This worked fine for me:

Moralis.Cloud.define("getIt", async (request) => {
    var results = ""
 let URL= 'https://api.coingecko.com/api/v3/coins/velas?localization=false&tickers=false&community_data=false&developer_data=false&sparkline=false'
 
return  Moralis.Cloud.httpRequest({
  url: URL,
    followRedirect:true
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
    results = JSON.parse(httpResponse.text)
    
    return results
    
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});
  
  });

That worked great! Thanks again!