In cloud function, im trying to put token_uri into EthNFTTransfers class so that my getItems function can call token.token_uri.
my getAndRenderItemData function:
getAndRenderItemData = (item, renderFunction) => {
fetch(item.tokenUri)
.then(response => response.json())
.then(data => {
item.name = data.name;
item.description = data.description;
item.image = data.image;
renderFunction(item);
console.log(item.tokenUri);
})
}
I get Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
. This is my fetch api method as it cant find the path, however the console.log(item.tokenUri)
shows each tokenUri:
This is my getItems cloudFunction:
*Note, i tried calling the Item class in the const query
as Item
. I dont know how to do this exactly, but im trying to get the metadataFilePath as the tokenUri. If that makes sense?
Moralis.Cloud.define("getItems", async (request) => {
const query = new Moralis.Query("ItemsForSale", "Item");
query.notEqualTo("isSold", true);
if (request.user){
query.notContainedIn("token.owner_of", request.user.attributes.accounts);
}
query.select("uid", "tokenAddress", "tokenId", "askingPrice", "token.token_uri", "token.symbol", "token.owner_of", "user.username", "user.avatar");
const queryResults = await query.find({ useMasterKey: true });
const results = [];
for (let i = 0; i < queryResults.length; ++i) {
if (!queryResults[i].attributes.token || !queryResults[i].attributes.user) continue;
results.push({
"uid": queryResults[i].attributes.uid,
"tokenId": queryResults[i].attributes.tokenId,
"tokenAddress": queryResults[i].attributes.tokenAddress,
"askingPrice": queryResults[i].attributes.askingPrice,
"symbol": queryResults[i].attributes.token.symbol,
"tokenUri": queryResults[i].attributes.token.token_uri,
"ownerOf": queryResults[i].attributes.token.owner_of,
"tokenObjectId": queryResults[i].attributes.token.id,
"sellerUsername": queryResults[i].attributes.user.attributes.username,
"sellerAvatar": queryResults[i].attributes.user.attributes.avatar,
});
}
return results;
});
it calls my ItemsForSale function which calls from the EthNFTTransfers class from my db:
Moralis.Cloud.beforeSave("ItemsForSale", async (request) => {
const query = new Moralis.Query("EthNFTTransfers");
query.equalTo("token_address", request.object.get('tokenAddress'));
query.equalTo("token_id", request.object.get('tokenId'));
const object = await query.first();
if (object){
const owner = object.attributes.owner_of;
const userQuery = new Moralis.Query(Moralis.User);
userQuery.equalTo("accounts", owner);
const userObject = await userQuery.first({ useMasterKey:true });
if (userObject){
request.object.set('user', userObject);
}
request.object.set('token', object);
}
});
The tokenUri is not in the EthNFTTransfers class,
But the metadataFilePath is within the Item class.
I know i may be overcomplicating this, but Iām just stuck.
This is the response i currently get:
Notice that the result does not show tokenUri (or anything that shares the token.attribute path, such as token_uri, symbol, owner_of).