I added cloud function to fix CORS errors but now i am getting back this errors:
Code:
async function getSearchNFTs(){
try{
let address = await callAPI(document.getElementById('addressInput').value);
console.log("in nfts, " + address);
const nfts = await allNFTs(address);
//Loop over the array
if(nfts.length == 0){
alert("Address invalid or it doesn't exist - try different one!")
$("#searchResults").empty();
$("#addressOrDomain").html("Address does not exsist, please try with another one!");
$("#numOfNFTs").html("Address does not exsist, please try with another one!");
$("#searchResults").html("<strong>Address does not have any NFTs, please try with other Address.</strong>");
$("#searchResultsDiv").show();
$("#searchAccordion").show();
}else{
$("#searchResults").empty();
$("#numOfNFTs").val("");
$("#addressOrDomain").val("");
nfts.forEach(async (e) => {
console.log(e);
let url = e.token_uri;
console.log(url)
const params = { theUrl: url }
const metadata = await Moralis.Cloud.run("fetchJSON", params);
console.log(metadata);
let data = metadata.data;
console.log(data)
let currentDiv = document.getElementById("searchResults");
let content = `
<div class="card" style="width: 18rem;">
<img src="${fixURL(data.image)}" class="card-img-top" id="img" alt="${data.name}">
<div class="card-body">
<h4 class="card-title">${data.name}</h4><hr>
<span class="card-text"><strong>Collection:</strong><span class="data">${e.name}</span><br>
<span class="card-text"><strong>Blockchain:</strong><span class="data">${e.chain}</span><br>
<span class="card-text"><strong>NFT Type:</strong><span class="data">${e.contract_type}</span></span>
</div>
</div>
`
currentDiv.innerHTML += content;
})
$("#addressOrDomain").html(address + ".");
$("#numOfNFTs").html((nfts.length -1) + " NFTs.");
$("#searchResultsDiv").show();
$("#searchAccordion").show();
}
}catch (err) {
console.log(err);
}
}
Cloud code:
//HTTP Request:
Moralis.Cloud.define("fetchJSON", async (request) => {
return Moralis.Cloud.httpRequest({
method: "GET",
url: request.params.theUrl,
})
});