Cloud Function does not return result to API console (?)

Hi ,
I have this function defined in the clouds:

Moralis.Cloud.define("get1", (request) => {
  const logger = Moralis.Cloud.getLogger();
  logger.info("Now returning 1");
  return 1;
});

then, in the API js_console, I call this code by this:

const _get1 = async() => {
  console.log("waiting for 1")
  const params =  {};
  const one = await Parse.Cloud.run("get1", params);
  console.log("GOT: ",one)   // Problem is here: this code never runs in the API/js_console (but in app it runs OK)
}

_get1();

Looking to the to the Core/Logs it seems no problems; Code runs with Result:1
in app code runs OK.

But in the api_console/js_console: The result never shown up (?!)
Only see this:

Log":{ 1 item
“result”: string"waiting for 1"
}

1 Like

Hi @tungtien,

Thanks for reaching out.

In-built consoles have different contexts and executional environments. So, when you wrap your code into a function and try to call it it behaves differently.

Just write your code without the function wrapper –

  console.log("waiting for 1")
  const params =  {};
  const one = await Parse.Cloud.run("get1", params);
  console.log("GOT: ",one

This way you can get the result.

Otherwise include the await keyword before you call your function.

const _get1 = async() => {
const params =  {};
console.log("waiting for 1")
   var one;
  one = await Parse.Cloud.run("get1", params);
  console.log("GOT: ", one)
    // Problem is here: this code never runs in the API/js_console (but in app it runs OK)
}

await _get1();

Why does this work? Well, check out a great explanation over here – https://stackoverflow.com/questions/48036038/javascript-async-function-console-log-the-returned-data

Hope this helps. :slight_smile:

Happy BUIDLing! :smiley:

1 Like

thanks @malik! Really appreciate your help

1 Like

By the way, I have another question here, hope you can help on this too :slight_smile:

This has been answered by @Yomoo ! Let’s continue the conversation over there. :slight_smile: