NFTOwners beforesave function failing after update

I used to have a before save function to fetch the metadata of NFT into the NFTOwners used to work perfectly until a recent update that broke it at some point the log said it was failing at the require https line says mod require is not defined. right now its having another error

I’ve tried to pull in the metadata through the web3api as follow but no errors and no luck either

var dataFromURI = await Moralis.Web3API.token.getTokenIdMetadata({ address: tokenAddress, token_id: tokenId, chain: ‘bsc testnet’}).then((result) => {
return result;
});

any body can shed some light what am I doing wrong or how to go now about achieving same result again.

Moralis.Cloud.beforeSave(“BscNFTOwners”, async (request) => {
const tokenAddress = request.object.get(‘token_address’);
const tokenId = request.object.get(‘token_id’);
const tokenUri = request.object.get(‘token_uri’);
if (!tokenUri) return;
var dataFromURI = await httpGet(tokenUri);
request.object.set(“metadata”, dataFromURI);
});

function httpGet(uri) {
const https = require(‘https’);

return new Promise ((resolve, reject) => {
	https.get(uri, (response) => {
	let data = '';
	response.on('data', (chunk) => { data += chunk; });
	response.on('end', () => { resolve(data); });
	}).on("error", (error) => { reject(error); });
}); 

}

if you use something like:

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);
  });

instead of https works for you?

no metadata and no errors

you may have to change that code a little, I don’t know exactly if it works how I pasted there,
what is an example of url that you try?

token uri https://ipfs.moralis.io:2053/ipfs/QmdFfRraiNPbJ4PcTJ8AoXe37ixvkAZJ4JZp3tRCc15j3G

right now it’s just in pending balance as I said no errors shows in console only that it’s pending.

ok, tested this and it looks to work:

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);
  });
 }); 

mine doesn’t even if I put it on it’s own function this is the error I’m getting now when I run it

2021-10-18T17:49:30.738Z - Error: Invalid function: “get_token_metadata”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:119:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (internal/process/task_queues.js:95:5)

Maybe there is some error somewhere, if you add my function in you cloud code I expect it to work.

as I said I did add it exactly same with static url still same result

If you copy exactly my code you get that error? Other functions work well?

yes

2021-10-18T18:32:18.462Z - Error: Invalid function: “get_token_uri”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:119:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (internal/process/task_queues.js:95:5)

Strange, are you sure you are connected to the right Moralis server? You get same error if you put there a random function name?

yes sure it’s the correct server I’m just initiating and running from console at this point

this does work anyway so I might just change my before save to include it.

Moralis.Cloud.define(“testMetadata”, async (request) => {
const { chain, tokenAddress, tokenId, } = request.params;
return await Moralis.Web3API.token.getTokenIdMetadata({ address: tokenAddress, token_id: tokenId, chain: chain}).then((result) => {
return result;
});
});

well now trying to incorportate the very same code into the before save results in

  1. 2021-10-18T21:08:30.535Z - beforeSave failed for BscNFTOwners for user undefined: Input:

{“name”:“Blockchain Factory”,“symbol”:“BLCKCHN”,“token_uri”:“https://ipfs.moralis.io:2053/ipfs/QmWantQC68Re6vWpn1PktgtGJoe1mPEJ71yvNdatys1j1K",“token_id”:“124”,“token_address”:“0x668aed884e1ebfa5dab26fc80ab0f8ecea59aa2b”,“owner_of”:“0x4771ff1f558b3cc9996ec030fca95fd672425ef6”,“block_number”:13343001,“amount”:“1”,“contract_type”:"ERC721”} Error: {“message”:"[object Object]",“code”:141}

here is the beforeSave()

Moralis.Cloud.beforeSave(“BscNFTOwners”, async (request) => {
const tokenAddress = request.object.get(‘token_address’);
const tokenId = request.object.get(‘token_id’);
const tokenUri = request.object.get(‘token_uri’);
if (!tokenUri) return;
// var dataFromURI = await httpGet(tokenUri);
var dataFromURI = await Moralis.Web3API.token.getTokenIdMetadata({ address: tokenAddress, token_id: tokenId, chain: ‘bsc testnet’}).then((result) => {
return result.metadata;
});

request.object.set("metadata", dataFromURI);

});

you could add some lines with logger.info(JSON.stringify(variable_name)); to see how far it gets in execution

even with ur code it’s not returning anything useful here is a sample

  1. 2021-10-18T21:31:49.200Z -
  2. 2021-10-18T21:31:49.200Z -
  3. 2021-10-18T21:31:49.200Z - error

the result could also depend on the url that it tries to access, sometimes that url could not work

except that all and every URL which is just files on the server itself btw not working I’m kind of giving up here this is killing the site that is using it already.

Can you add more logging? like the url that it is tried to get accessed before it tries to access it.