Calling nft metadat via cloud function

@cryptokid, following up on the above. i am getting currently below output by using .then(res => console.log(res)):

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

now, I would like to parse the body. i tried the below, but i am getting always empty results back. any idea how to fix? let me know if you need further information:

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

here is the log:

2021-11-14T13:59:37.816Z - Ran cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"0"}
  Result: undefined
2021-11-14T13:59:37.815Z -
2021-11-14T13:59:37.814Z - error
2021-11-14T13:59:37.806Z - Ran cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"1"}
  Result: undefined
2021-11-14T13:59:37.805Z -
2021-11-14T13:59:37.804Z - error
2021-11-14T13:59:37.764Z - 0000000000000000000000000000000000000000000000000000000000000001
2021-11-14T13:59:37.759Z - 0000000000000000000000000000000000000000000000000000000000000000
2021-11-14T13:59:37.701Z - Ran cloud function getAllTokenIds for user undefined with:
  Input: {"address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","chain":"rinkeby"}
  Result: {"total":2,"page":0,"page_size":500,"result":[{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"1","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000001.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/photo.jpg\",\n    \"description\": \"New York City, Manhatten, Summer 2012\",\n    \"name\": \"A very dynamic city\",\n    \"external_url\": \"https://antonmartinsson.com/\"\n\n}","synced_at":"2021-11-06T13:28:51.727Z","amount":"2","name":null,"symbol":null},{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"0","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000000.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/artwork.png\",\n    \"description\": \"The art work of Lorian \",\n    \"name\": \"Lorian artwork\"\n}","synced_a... (truncated)

How do you call that getNFT function?
What do you see in your browser console, any errors there?

Can you add some logging before that return?
It looks like this cloud function doesn’t return anything

1 Like

already done. this is my log output. it says undefined not sure why.

2021-11-14T13:59:37.816Z - Ran cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"0"}
  Result: undefined
2021-11-14T13:59:37.815Z -
2021-11-14T13:59:37.814Z - error
2021-11-14T13:59:37.806Z - Ran cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"1"}
  Result: undefined
2021-11-14T13:59:37.805Z -
2021-11-14T13:59:37.804Z - error
2021-11-14T13:59:37.764Z - 0000000000000000000000000000000000000000000000000000000000000001
2021-11-14T13:59:37.759Z - 0000000000000000000000000000000000000000000000000000000000000000
2021-11-14T13:59:37.701Z - Ran cloud function getAllTokenIds for user undefined with:
  Input: {"address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","chain":"rinkeby"}
  Result: {"total":2,"page":0,"page_size":500,"result":[{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"1","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000001.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/photo.jpg\",\n    \"description\": \"New York City, Manhatten, Summer 2012\",\n    \"name\": \"A very dynamic city\",\n    \"external_url\": \"https://antonmartinsson.com/\"\n\n}","synced_at":"2021-11-06T13:28:51.727Z","amount":"2","name":null,"symbol":null},{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"0","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000000.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/artwork.png\",\n    \"description\": \"The art work of Lorian \",\n    \"name\": \"Lorian artwork\"\n}","synced_a... (truncated)

no errors if i am calling getNFT function. I am receiving below output in my console:

Response {type: 'cors', url: 'https://feciihxhdokr.usemoralis.com:2053/server/fu…=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=1', redirected: false, status: 200, ok: true, …}

What does that error line from logs means?

not sure. below 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);
        });
  });

perhpas it is complaing because i have multiple mints in my opensea account? below the link to my opensea account:
https://testnets.opensea.io/0x1b64bc818e06a21ca94c1d918d81e0615fe3073e

You can add here a line with: logger.info(JSON.stringify(httpResponse))

1 Like

getting the below output:

2021-11-14T14:42:08.708Z - Ran cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"1"}
  Result: undefined
2021-11-14T14:42:08.707Z - {"status":404,"headers":{"date":"Sun, 14 Nov 2021 14:42:08 GMT","content-type":"text/html","transfer-encoding":"chunked","connection":"close","cf-cache-status":"DYNAMIC","expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"","server":"cloudflare","cf-ray":"6ae10133fb9e178a-FRA"},"buffer":{"type":"Buffer","data":[60,104,116,109,108,62,13,10,60,104,101,97,100,62,60,116,105,116,108,101,62,52,48,52,32,78,111,116,32,70,111,117,110,100,60,47,116,105,116,108,101,62,60,47,104,101,97,100,62,13,10,60,98,111,100,121,62,13,10,60,99,101,110,116,101,114,62,60,104,49,62,52,48,52,32,78,111,116,32,70,111,117,110,100,60,47,104,49,62,60,47,99,101,110,116,101,114,62,13,10,60,104,114,62,60,99,101,110,116,101,114,62,110,103,105,110,120,47,49,46,49,56,46,48,32,40,85,98,117,110,116,117,41,60,47,99,101,110,116,101,114,62,13,10,60,47,98,111,100,121,62,13,10,60,47,104,116,109,108,62,13,10]},"text":"<html>\r\n<head><title>404 Not Found</title></head>\r\n<body>\r\n<center><h1>404 Not Found</h1></center>\r\n<hr><center>nginx/1.18.0 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n","level":"info"}
2021-11-14T14:42:08.704Z -
2021-11-14T14:42:08.702Z - error
2021-11-14T14:42:08.659Z - Ran cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"0"}
  Result: undefined
2021-11-14T14:42:08.659Z - {"status":404,"headers":{"date":"Sun, 14 Nov 2021 14:42:08 GMT","content-type":"text/html","transfer-encoding":"chunked","connection":"close","cf-cache-status":"DYNAMIC","expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"","server":"cloudflare","cf-ray":"6ae10133ef7a430f-FRA"},"buffer":{"type":"Buffer","data":[60,104,116,109,108,62,13,10,60,104,101,97,100,62,60,116,105,116,108,101,62,52,48,52,32,78,111,116,32,70,111,117,110,100,60,47,116,105,116,108,101,62,60,47,104,101,97,100,62,13,10,60,98,111,100,121,62,13,10,60,99,101,110,116,101,114,62,60,104,49,62,52,48,52,32,78,111,116,32,70,111,117,110,100,60,47,104,49,62,60,47,99,101,110,116,101,114,62,13,10,60,104,114,62,60,99,101,110,116,101,114,62,110,103,105,110,120,47,49,46,49,56,46,48,32,40,85,98,117,110,116,117,41,60,47,99,101,110,116,101,114,62,13,10,60,47,98,111,100,121,62,13,10,60,47,104,116,109,108,62,13,10]},"text":"<html>\r\n<head><title>404 Not Found</title></head>\r\n<body>\r\n<center><h1>404 Not Found</h1></center>\r\n<hr><center>nginx/1.18.0 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n","level":"info"}
2021-11-14T14:42:08.654Z -
2021-11-14T14:42:08.652Z - error
2021-11-14T14:42:08.608Z - 0000000000000000000000000000000000000000000000000000000000000001
2021-11-14T14:42:08.597Z - 0000000000000000000000000000000000000000000000000000000000000000
2021-11-14T14:42:08.468Z - Ran cloud function getAllTokenIds for user undefined with:
  Input: {"address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","chain":"rinkeby"}
  Result: {"total":2,"page":0,"page_size":500,"result":[{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"1","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000001.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/photo.jpg\",\n    \"description\": \"New York City, Manhatten, Summer 2012\",\n    \"name\": \"A very dynamic city\",\n    \"external_url\": \"https://antonmartinsson.com/\"\n\n}","synced_at":"2021-11-06T13:28:51.727Z","amount":"2","name":null,"symbol":null},{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"0","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000000.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/artwork.png\",\n    \"description\": \"The art work of Lorian \",\n    \"name\": \"Lorian artwork\"\n}","synced_a... (truncated)
2021-11-14T14:42:07.192Z - Ran cloud function getPluginSpecs for user jBmojqUorYpKMqfPw4YdE0dd with:
  Input: {}
  Result: []
2021-11-14T14:42:05.265Z - Initialized plugin ./evm/balances
2021-11-14T14:42:05.263Z - Initialized plugin ./evm/historical/transactions
2021-11-14T14:42:05.255Z - Initialized plugin ./convenience/index
2021-11-14T14:42:04.880Z - Initialized plugin ./evm/consumer
2021-11-14T14:42:04.208Z - Initialized plugin ./server
2021-11-14T14:42:04.206Z - Initialized plugin ./evm/historical/accountApiFunctions
2021-11-14T14:42:04.203Z - Initialized plugin ./evm/historical/historicalApiFunctions
2021-11-14T14:42:03.416Z - Parse LiveQuery Server starts running

It looks like a 404 not found error, like when the url doesn’t exist.

mhh, ok. but i don’t understand what is missing here? e.g. below URL is working if entered manually in the browser. any idea how to fix?

You could log to see what is the exact url that is trying to access.

i printed below url for my first nft. if entered manually in the browser, it returned empty.

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

so the only solution would be to create a new server, new test account and try it again?

No, I mean in your cloud function, to log what url is trying to access with that http request.

ok, can you please help me by providing a snip of code? i am bit confused how to do it.

You compute this url before using it in that function and you use logger.info(url) to see what you have there

1 Like

below is my cloud log:

021-11-14T15:16:46.437Z - ReferenceError: url is not defined
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:107:21), <anonymous>:1:507)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
2021-11-14T15:16:46.433Z - Failed running cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"0"}
  Error: {"message":"url is not defined","code":141}
2021-11-14T15:16:46.428Z - ReferenceError: url is not defined
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:107:21), <anonymous>:1:507)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
2021-11-14T15:16:46.419Z - Failed running cloud function getNFT for user undefined with:
  Input: {"_ApplicationId":"ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC","nftId":"1"}
  Error: {"message":"url is not defined","code":141}
2021-11-14T15:16:46.369Z - 0000000000000000000000000000000000000000000000000000000000000000
2021-11-14T15:16:46.359Z - 0000000000000000000000000000000000000000000000000000000000000001
2021-11-14T15:16:46.221Z - Ran cloud function getAllTokenIds for user undefined with:
  Input: {"address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","chain":"rinkeby"}
  Result: {"total":2,"page":0,"page_size":500,"result":[{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"1","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000001.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/photo.jpg\",\n    \"description\": \"New York City, Manhatten, Summer 2012\",\n    \"name\": \"A very dynamic city\",\n    \"external_url\": \"https://antonmartinsson.com/\"\n\n}","synced_at":"2021-11-06T13:28:51.727Z","amount":"2","name":null,"symbol":null},{"token_address":"0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd","token_id":"0","contract_type":"ERC1155","token_uri":"https://rmcpzgnm5yhv.usemoralis.com/0000000000000000000000000000000000000000000000000000000000000000.json","metadata":"{\n    \"image\": \"https://rmcpzgnm5yhv.usemoralis.com/artwork.png\",\n    \"description\": \"The art work of Lorian \",\n    \"name\": \"Lorian artwork\"\n}","synced_a... (truncated)```

I updated my cloud function as follow: 

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(url);
    logger.info("error");
    logger.info(httpResponse);
  	logger.info(JSON.stringify(httpResponse));

I mean, you define the url above this line, so that you can log it

1 Like