Use cloud function on IDE

Hi guys, I want to use the new cloud function thing from Moralis on my IDE. When I enter the command it prompts for a secret API key and I am not sure what or where to find this I am getting error 403 no matter the key I enter

Where can I find the correct one?
Thanks

You can find both CLI api key and CLI api secret on your account profile settings in the keys sector

Okay thanks got it. Iโ€™m sorry but this is extremely confusing to me about the folder in the command line. I put the path of the folder where I create a .js file where I will write functions? And can I see if functions have correctly been uploaded to my server?

Okay I start to understand. Also why fetch no longer works for httprequest? Using Moralis.httprequest gives me empty response even though it should not.

You can see that from the legacy UI, once itโ€™s uploaded successfully, itโ€™s all fine

It should work properly possibly you did something wrongly. You can try log the response with logger.info and also try catch possible errors in a catch block to be sure no errors

Thanks for your reply. This is the code Iโ€™m using in my cloud function:

Moralis.Cloud.define("fetchJSON", function(req) {
    let test = req.params.theUrl;
    let results = "";
    Moralis.Cloud.httpRequest({
        url:test
    }).then((res) => {
        results = res;
        logger.info(res)
    }).catch((err) => {
        results = err;
    })
    return results;
});

I call the function with an onlick button. I tried to log the url and I get the correct one so the response should not be empty.
(the url for reference: https://ipfs.moralis.io:2053/ipfs/Qmf9Ck4JhiyWeDzbJtXQufjbankmd5Tzhr8JXCZ7wQ6iWR/test/0.json)

EDIT: I am getting the correct response when logging (res.text) but the result is not correctly returned as I am getting nothing

You can make your code async having such

Moralis.Cloud.define("fetchJSON", async (req) => {

And you can await the request by adding await keyword in front of this

Having such

await Moralis.Cloud.httpRequest({

You should also return results.text if youโ€™re trying to send back the response from the request

Thanks, this is my updated function:

Moralis.Cloud.define("fetchJSON", async function(req) {
    let test = req.params.theUrl;
    await Moralis.Cloud.httpRequest({
        url: 'https://ipfs.moralis.io:2053/ipfs/Qmf9Ck4JhiyWeDzbJtXQufjbankmd5Tzhr8JXCZ7wQ6iWR/test/0.json'
    }).then((res) => {
        logger.info(res.text);
        return res.text;
    }).catch((err) => {
        logger.info(err);
    })
});

The cloud function seems to work fine because my logger.info() returns the correct information (what I am expecting)
But on the front end when I do a console.log() with the response I get undefined.
This is the code I use in the frontend:

  const cloudMeta = async(_uri) => {
    let params = {theUrl : _uri};
    const metadata = await Moralis.Cloud.run("fetchJSON", params);
    console.log(metadata);
  }
      <button onClick={() => cloudMeta('addUrlLater')}>fetch info</button>

EDIT: in my cloud function console I have the following:

2022-07-22T20:43:49.032Z - Ran cloud function fetchJSON for user J6m8NuJxsONa6WhhHdBmjrod with:
  Input: {"theUrl":"https://ipfs.moralis.io:2053/ipfs/Qmf9Ck4JhiyWeDzbJtXQufjbankmd5Tzhr8JXCZ7wQ6iWR/test/0.json"}
  Result: undefined


2022-07-22T21:09:50.723Z - {"eventName":"test","eventDate":"2022-07-22T17:16:49.791Z","eventLocation":{"value":"FR","label":"France"},"NumberOfTicket":"100","attributs":{"attribut":"10"}}

as you can see the first log shows result as undefined which explains why in fmy front end I have undefined but the second log shows the json I am expecting to get which means the http request works correctly.
I am kinda lost ngl

This is due to the return statement. The confusion is due to scopes in js. Following your initial example, you can have such

Moralis.Cloud.define("fetchJSON", async (req) => {
  let test = req.params.theUrl;
  let results = "";
  await Moralis.Cloud.httpRequest({
    url: test,
  })
    .then((res) => {
      results = res;
      logger.info(res);
    })
    .catch((err) => {
      results = err;
    });
  return results.text;
});
1 Like

Thank you itโ€™s working fine now.