getalltokenIds is working. any idea?
const options = { address: "0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd", chain: "rinkeby" };
let NFTs = await Moralis.Web3API.token.getAllTokenIds(options);
getalltokenIds is working. any idea?
const options = { address: "0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd", chain: "rinkeby" };
let NFTs = await Moralis.Web3API.token.getAllTokenIds(options);
You can use the Moralis.Web3API
in cloud functions instead of HTTP requests
thank you @Yomoo. belwo is my cloud function. I would replace httpRequest with Moralis.Web3API, correct?
here is the address of NFT:
https://rinkeby.etherscan.io/tx/0x555e09822fcf31ed3f4f309908e2eca298a8cbd64162d63fa5151d941fc2faa5
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"})
.then(function(httpResponse){
return httpResponse.text;
})
})
@Yomoo or @cryptokid, can you please let me know how to solve the above? should i change anything in my code? Let me know if you need more information. thank you
thank you @cryptokid, but I am a bit lost here. How would you replace your function with my cloud function? if there is a good video, please let me know. thank you
my:
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"})
.then(function(httpResponse){
return httpResponse.text;
})
})
yours:
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);
});
});
I am calling the cloud function in my main.js as follow:
unction fetchNFTMetadata(NFTs) {
for (let i = 0; i < NFTs.length; i++) {
let nft = NFTs[i];
let id = nft.token_id;
//Call Moralis cloud funciton -> static json file
fetch("https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=" + id)
//.then(res => res.json())
.then(res => console.log(res))
}
}
you can combine those 2 functions, you can get the part that computes the url from your function and get the part with Moralis.Cloud.httpRequest from the other function
thank you @cryptokid . i tried something like that but still receiving an error. what am i missing?
Moralis.Cloud.define("get_token_uri", (request) => {
for (let i = 0; i < NFTs.length; i++) {
let nft = NFTs[i];
let id = nft.token_id;
let url = "https://ipfs.moralis.io:2053/ipfs/QmdFfRraiNPbJ4PcTJ8AoXe37ixvkAZJ4JZp3tRCc15j3Ghttps://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=" + id
}
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);
});
});
I donât know what this code was suppose to do, it has a for loop that only initialises url variable, that url variable is initialised with two https strings + id
I wouldnât expect that code to work.
you can leave that for in your js, and only change the code of you cloud function to use the part that starts with return Moralis.Cloud.httpRequest(
in this function.
@cryptokid what would be my url?
serverUrl: 'https://feciihxhdokr.usemoralis.com:2053/server',
appId: 'ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC'
and this 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);
});
});
I am getting an error. i think my url is not correct. what am i missing? any idea?
In your logs, there are messages about syntax problems in your code. You have not valid JS code
We donât understand what does you code do. Please explain
Syntax problem is there:
"url": {"https://feciihxhdokr.usemoralis.com/" + paddedHex + ".json" },
Change it to:
url: `${"https://feciihxhdokr.usemoralis.com/" + paddedHex + ".json"}`,
thank you @Yomoo. i am not receiving any error message and I am receiving the following output:
Response {type: 'cors', url: 'https://feciihxhdokr.usemoralis.com:2053/server/fuâŚ=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=0', redirected: false, status: 200, ok: true, âŚ}body: (...)bodyUsed: falseheaders: Headers {}ok: trueredirected: falsestatus: 200statusText: ""type: "cors"url: "https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=0"[[Prototype]]: Response
I am trying now to parse the result in a JSON format, similar as shown in the tutorial âUltimate NFT Programming Tutorial -FULL COURSEâ if I execute:
.then(res => console.log(res))
it works. as soon as I am executing:
//.then(res => res.json())
//.then(res => JSON.parse(res.result))
I am getting an error, empty result. Can you please provide me with a work around? It is in the video in min 110, 111.
what is the code that you use now?
thatâs my current code.
Moralis.start({
serverUrl: 'https://feciihxhdokr.usemoralis.com:2053/server',
appId: 'ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC'
})
function fetchNFTMetadata(NFTs) {
for (let i = 0; i < NFTs.length; i++) {
let nft = NFTs[i];
let id = nft.token_id;
//Call Moralis cloud funciton -> static json file
fetch("https://feciihxhdokr.usemoralis.com:2053/server/functions/getNFT?_ApplicationId=ePc0HyzXQdJ3ytVt8J4zhNDWS0WCFSANX6z87xIC&nftId=" + id)
//.then(res => res.json())
//.then(res => JSON.parse(res.result))
.then(res => console.log(res))
}
}
async function initialzeApp() {
let currentUser = Moralis.User.current();
if (!currentUser) {
currentUser = await Moralis.Web3.authenticate();
}
//alert("user is signed in" + currentUser.get('ethAddress'));
//console.log(currentUser.get('ethAddress'));
const options = { address: "0xc638f3a8ecd0579f9e3a75693f87adc4c1357ffd", chain: "rinkeby" };
let NFTs = await Moralis.Web3API.token.getAllTokenIds(options);
console.log("Hello World");
console.log(NFTs);
fetchNFTMetadata(NFTs.result);
}
initialzeApp();
Wouldnât be easier for use something like
x = await Moralis.Cloud.run(âgetFFTâ, {nftId : â10â})
@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))