Ultimate NFT Programming course - help

Hi all, recently joined the community and looking to join the academy as well.
Love your work guys. I hope somebody can help debug this.

I got carried away following Philip’s Ultimate NFT course & got stuck for 2 days at:

function fetchNFTMetadata(NFTs) {
  let promises = [];
  for (let i; i < NFTs.length; i++) {
    let nft = NFTs[i];
    let id = nft.token_id;
    // Calling Moralis Cloud function for static JSON file.
      promises.push(fetch("https://ojuwkzwm1af5.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=zT4AIsHtDbdrZn0J90ciEjFDraFmddBz42Dxqbmw&nftId=" +id)
      .then(res => res.json())
      .then(res => JSON.parse(res.result))
      .then(res => {nft.metadata = res})
      .then( () => {return nft;}))
return Promise.all(promises);

async function initializeApp() {
  let currentUser = Moralis.User.current();
  if (!currentUser) {
    currentUser = await Moralis.Web3.authenticate();

  const options = { address: "0x10ce6c0a658a1ffd4c5af3996a628ad6d49715ae", chain: "BSC" };

  let NFTs = await Moralis.Web3API.token.getAllTokenIds(options);
  let NFTWithMetadata = await fetchNFTMetadata(NFTs.result);


All I get is a
Screenshot 2021-12-28 at 13.00.57

Would anybody spare a second please?

I can manage to return unsanitised object, but I can’t manage to sanitse in order to render the data on screen.

what is the problem that you have?

That was incredibly quick. If you compare Philip’s console output (Y2 link), he’s logging sanitised object.

I’ve tried everything and I always get empty Array as a result.

what you have in NFTs variable?

did you add that cloud function named getNFT ?

NFTs variable returns an object -

Cloud function exactly as Philip’s

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

are there those .json files accessible?

you can try to print the path to see if it it is accessible (you can use logger.info), you can also check if there are any error on that Moralis.Cloud.httpRequest

those are accessible according to logs & if I type out URL it returns the correct json file.

2021-12-28T20:08:11.824Z - 0000000000000000000000000000000000000000000000000000000000000001
2021-12-28T20:08:11.706Z - 0000000000000000000000000000000000000000000000000000000000000002
2021-12-28T20:08:11.567Z - 0000000000000000000000000000000000000000000000000000000000000003
2021-12-28T20:08:00.319Z - 0000000000000000000000000000000000000000000000000000000000000001
2021-12-28T20:08:00.181Z - 0000000000000000000000000000000000000000000000000000000000000002
2021-12-28T20:08:00.003Z - 0000000000000000000000000000000000000000000000000000000000000003
2021-12-28T20:01:19.868Z - 0000000000000000000000000000000000000000000000000000000000000001
2021-12-28T20:01:19.643Z - 0000000000000000000000000000000000000000000000000000000000000002

This is the logged object output from NFTs.result

Array(3) [ {…}, {…}, {…} ]
0: Object { token_address: "0x10ce6c0a658a1ffd4c5af3996a628ad6d49715ae", token_id: "3", amount: "3", … }
1: Object { token_address: "0x10ce6c0a658a1ffd4c5af3996a628ad6d49715ae", token_id: "2", amount: "1", … }
2: Object { token_address: "0x10ce6c0a658a1ffd4c5af3996a628ad6d49715ae", token_id: "1", amount: "3", … }
length: 3
<prototype>: Array []

let NFTWithMetadata = await fetchNFTMetadata(NFTs.result);
and if I console.log(NFTWithMetadata) <-- returns

Array []
length: 0
<prototype>: Array []

ok, it looks like those urls are accessible, you could add more logging to see what is the problem, or rewrite that code that calls that cloud function and make it simpler with await

Thank you for your prompt response.

Don’t mean to be annoying, but how would you solve this with await?

it wouldn’t solve it, it will be less code with await, but equivalent functionality

Thank you for your help :star_struck: