Calling nft metadat via cloud function

Hi,

I am receiving the below error:

https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=1

Is it me, the cloud function or moralis cloud which does not work?

this is the fetch: fetch(“https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=” + id)

1 Like

getalltokenIds is working. any idea?

const options = { address: "0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd", chain: "rinkeby" };
    let NFTs = await Moralis.Web3API.token.getAllTokenIds(options);

Hi @nani

You need to provide an address of NFT

You can use the Moralis.Web3API in cloud functions instead of HTTP requests

1 Like

thank you @Yomoo. belwo is my cloud function. I would replace httpRequest with Moralis.Web3API, correct?

here is the address of NFT:
https://rinkeby.etherscan.io/tx/0x555e09822fcf31ed3f4f309908e2eca298a8cbd64162d63fa5151d941fc2faa5

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)
  logger.info(paddedHex)
  return Moralis.Cloud.httpRequest({url: "https://feciihxhdokr.usemoralis.com/"+ paddedHex + ".json"})
  .then(function(httpResponse){
    return httpResponse.text;
  })
 
})

@Yomoo or @cryptokid, can you please let me know how to solve the above? should i change anything in my code? Let me know if you need more information. thank you

1 Like

you can try this syntax: NFTOwners beforesave function failing after update

1 Like

thank you @cryptokid, but I am a bit lost here. How would you replace your function with my cloud function? if there is a good video, please let me know. thank you
my:

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)
  logger.info(paddedHex)
  return Moralis.Cloud.httpRequest({url: "https://feciihxhdokr.usemoralis.com/"+ paddedHex + ".json"})
  .then(function(httpResponse){
    return httpResponse.text;
  })
 
})	

yours:

Moralis.Cloud.define("get_token_uri",  (request) => {
    let url = "https://ipfs.moralis.io:2053/ipfs/QmdFfRraiNPbJ4PcTJ8AoXe37ixvkAZJ4JZp3tRCc15j3G";
    return Moralis.Cloud.httpRequest({
      "url": url,
      "headers": {
          'method': 'GET',
          'accept': 'application/json'
         }
     }).then(function(httpResponse){
      return httpResponse.data;
    },function(httpResponse){
      logger.info("error");
      logger.info(httpResponse);
    });
   });

I am calling the cloud function in my main.js as follow:

unction fetchNFTMetadata(NFTs) {
    for (let i = 0; i < NFTs.length; i++) {
        let nft = NFTs[i];
        let id = nft.token_id;
        //Call Moralis cloud funciton -> static json file 
        fetch("https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=" + id)
            //.then(res => res.json())
            .then(res => console.log(res))
    }
}

1 Like

you can combine those 2 functions, you can get the part that computes the url from your function and get the part with Moralis.Cloud.httpRequest from the other function

1 Like

thank you @cryptokid . i tried something like that but still receiving an error. what am i missing?


Moralis.Cloud.define("get_token_uri",  (request) => {

    for (let i = 0; i < NFTs.length; i++) {
        let nft = NFTs[i];
        let id = nft.token_id;
        let url = "https://ipfs.moralis.io:2053/ipfs/QmdFfRraiNPbJ4PcTJ8AoXe37ixvkAZJ4JZp3tRCc15j3Ghttps://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=" + id
    }
    return Moralis.Cloud.httpRequest({
      "url": url,
      "headers": {
          'method': 'GET',
          'accept': 'application/json'
         }
     }).then(function(httpResponse){
      return httpResponse.data;
    },function(httpResponse){
      logger.info("error");
      logger.info(httpResponse);
    });
});
1 Like

I don’t know what this code was suppose to do, it has a for loop that only initialises url variable, that url variable is initialised with two https strings + id
I wouldn’t expect that code to work.

you can leave that for in your js, and only change the code of you cloud function to use the part that starts with return Moralis.Cloud.httpRequest( in this function.

2 Likes

@cryptokid what would be my url?

serverUrl: 'https://feciihxhdokr.usemoralis.com:2053/server',
    appId: 'ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC'

and this is my cloud function:


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)
    logger.info(paddedHex)
        return Moralis.Cloud.httpRequest({
            "url": {"https://feciihxhdokr.usemoralis.com/" + paddedHex + ".json" },
        "headers": {
            'method': 'GET',
            'accept': 'application/json'
            }
        }).then(function(httpResponse){
        return httpResponse.data;
        },function(httpResponse){
        logger.info("error");
        logger.info(httpResponse);
        });
  });

I am getting an error. i think my url is not correct. what am i missing? any idea?

1 Like

In your logs, there are messages about syntax problems in your code. You have not valid JS code

We don’t understand what does you code do. Please explain :raised_hands:

Syntax problem is there:

"url": {"https://feciihxhdokr.usemoralis.com/" + paddedHex + ".json" },

Change it to:

url: `${"https://feciihxhdokr.usemoralis.com/" + paddedHex + ".json"}`,
1 Like

thank you @Yomoo. i am not receiving any error message and I am receiving the following output:

Response {type: 'cors', url: 'https://feciihxhdokr.usemoralis.com:2053/server/fu…=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=0', redirected: false, status: 200, ok: true, …}body: (...)bodyUsed: falseheaders: Headers {}ok: trueredirected: falsestatus: 200statusText: ""type: "cors"url: "https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=0"[[Prototype]]: Response

I am trying now to parse the result in a JSON format, similar as shown in the tutorial “Ultimate NFT Programming Tutorial -FULL COURSE” if I execute:

.then(res => console.log(res))

it works. as soon as I am executing:

 //.then(res => res.json())
            //.then(res => JSON.parse(res.result))

I am getting an error, empty result. Can you please provide me with a work around? It is in the video in min 110, 111.

what is the code that you use now?

that’s my current code.

Moralis.start({
    serverUrl: 'https://feciihxhdokr.usemoralis.com:2053/server',
    appId: 'ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC'
})



function fetchNFTMetadata(NFTs) {
    for (let i = 0; i < NFTs.length; i++) {
        let nft = NFTs[i];
        let id = nft.token_id;
        //Call Moralis cloud funciton -> static json file 
        fetch("https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=" + id)
            //.then(res => res.json())
            //.then(res => JSON.parse(res.result))
            .then(res => console.log(res))
    }
}


async function initialzeApp() {
    let currentUser = Moralis.User.current();
    if (!currentUser) {
        currentUser = await Moralis.Web3.authenticate();
    }
    //alert("user is signed in" + currentUser.get('ethAddress'));
    //console.log(currentUser.get('ethAddress'));


    const options = { address: "0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd", chain: "rinkeby" };
    let NFTs = await Moralis.Web3API.token.getAllTokenIds(options);
    console.log("Hello World");
    console.log(NFTs);
    fetchNFTMetadata(NFTs.result);



}


initialzeApp();

Wouldn’t be easier for use something like
x = await Moralis.Cloud.run(“getFFT”, {nftId : “10”})

1 Like