Cloud functions: unable to get NFT data

Cloud functions in BSC Testnet could not load tokenURIā€¦ please help.

during the trigger process (when sync function called to blockchain event)
I need to store the data about the NFT and try to get the NFT dataā€¦

Iā€™m trying different approaches:

  1. await Moralis.Web3API.native.runContractFunction({
    chain: ā€œ0x61ā€,
    address: ā€œ0xA798E8109Ba8D1382bc875964F92D54c7d2608daā€,
    function_name: ā€œtokenURIā€,
    abi: Moralis.Web3.abis.erc721,
    params: {tokenId: 3}
    });
  • returns undentified (previously in prod mode is was working fine)
  1. await Moralis.Cloud.httpRequest({
    method: ā€˜GETā€™,
    url: https://deep-index.moralis.io/api/v2/nft/0xA798E8109Ba8D1382bc875964F92D54c7d2608da/3,
    params: {
    chain: ā€œ0x61ā€,
    format: ā€˜decimalā€™,
    normalizeMetadata: ā€˜trueā€™
    },
    headers: {
    accept: ā€˜application/jsonā€™,
    ā€˜X-API-Keyā€™: ā€˜ā€¦ā€™
    }
    });
  • breaks in the cloud-code somehow and response - undentifiedā€¦

PLEASE ))) What is the working way of getting tokenURI for NFT from CloudCode
I donā€™t want to rewrite all the project using new tech stack any time Moralis upgrades :frowning:

If I execute 2nd function in job - it works fineā€¦ but when I call that code in trigger (on sync) method - it returns undefined

you are using a self hosted server?
are you returning something in that cloud code function?
you can try to add some logging to see what happens

no, not self-hostedā€¦ just Moralis server.
Yes ))) Iā€™m returning something and logging - thats how I undestand the undentified in response

how does the code for that cloud function looks like?

you can also try to call that cloud function by accessing it directly in the browser to see what it returns

Cloud Code looks like that:

log after that:

in the browser similar code (except Moralis.Cloudā€¦) executes correctly.

Also, in the Cloud JOB - that code executes correctlyā€¦

BUT Also that code in Cloud always return undefined:
image

the result is visible here:

Last code in browser also returns undefined:

can you paste that code?

what version of Moralis SDK you are using?

version of Moralis SDK: 1.9.0

const options = {
      chain: "0x61",
      address: "0xA798E8109Ba8D1382bc875964F92D54c7d2608da",
      function_name: "tokenURI",
      abi: [{
        "inputs": [{"internalType": "uint256", "name": "tokenId", "type": "uint256"}],
        "name": "tokenURI",
        "outputs": [{"internalType": "string", "name": "", "type": "string"}],
        "stateMutability": "view",
        "type": "function"
      }],
      params: {tokenId: 3}
    };
    //@ts-ignore
    const result = await Moralis.Web3API.native.runContractFunction(options);
    console.log(result)

it looks like I also get undefined in this case, with Moralis.Cloud.httpRequest it should work

you can try an example like this one

this works

curl -X 'POST' \
  'https://deep-index.moralis.io/api/v2/0xA798E8109Ba8D1382bc875964F92D54c7d2608da/function?chain=bsc%20testnet&function_name=tokenURI' \
  -H 'accept: application/json' \
  -H 'X-API-Key: API_KEY_HERE' \
  -H 'Content-Type: application/json' \
  -d '{
  "abi": [{
        "inputs": [{"internalType": "uint256", "name": "tokenId", "type": "uint256"}],
        "name": "tokenURI",
        "outputs": [{"internalType": "string", "name": "", "type": "string"}],
        "stateMutability": "view",
        "type": "function"
      }],
  "params": {"tokenId": "3"}
}'

=>

https://ipfs.moralis.io:2053/ipfs/QmNU9D7H6r4onzfDibpVH8ASAeB84Sn85XjiZ8qoEfuJi3

you should also try to self host a server, you have more control when you self host a server

The similar code is working on production and I want to fix it, and not to run from scratch ))

Am i doing something wrong here?:

const requestOptions = {
    method: 'POST',
    url: `https://deep-index.moralis.io/api/v2/0xA798E8109Ba8D1382bc875964F92D54c7d2608da/function?chain="0x61"&function_name=tokenURI`,
    body: {
      params: {
        "tokenId": 3
      },
      abi: [{
        "inputs": [{"internalType": "uint256", "name": "tokenId", "type": "uint256"}],
        "name": "tokenURI",
        "outputs": [{"internalType": "string", "name": "", "type": "string"}],
        "stateMutability": "view",
        "type": "function"
      }]
    },
    headers: {
      accept: 'application/json',
      'Content-Type': 'application/json',
      'X-API-Key': getAPIKey()
    }
  };

  const tokenUri = await Moralis.Cloud.httpRequest(requestOptions);

because that code fails in Cloud Codeā€¦

Ok. Iā€™m digging into that issue about 24h )))
(still same project works in prod mode)

Can you please, provide the simple code or link to it,
that I can use to get just simple tokenURI from NFT contract? )))

with Moralis.Web3API.native.runContractFunction(ā€¦) - all calls works fine, except those, that returns simple stringsā€¦

other methods works in Jobs and browsers, but not working in triggered Cloud-Codeā€¦

try to use the other syntax for httpRequest (that one that shows the errors and not this one with await, maybe tit is an error somewhere with that request

here try with ā€œ3ā€ instead of 3 (probably It doesnā€™t matter)

try to add some logging to see if the api key is the right one, or try to hardcode it