Rarible clone. Cloud functions - Unexepctedd identifier:

1.Hi there! I’m trying to clone rarible using your guide.
2. When I created a Ganachi server it gave me an error so I had to go with the Testnet (Rinkeby).
3)In video8 I open cloud functions and write this code:

Moralis.Cloud.define("getUserItems", async (request) => {
  
  const query = new Moralis.Query("EthTokenBalance");
  query.equalTo("contract_type", "ERC721");
  query.containedIn("owner_of", "request.user.attributes.accounts");
  const queryResults = await query.find();
  const results =[];
  
  for (let i = 0; i < queryResults.length; ++i) {
    results.push({
      "id":queryResults[i].attributes.objectId,
       "tokenid":queryResults[i].attributes.token_id,
       "tokenAddress":queryResults[i].attributes.token_address,
       "symbol":queryResults[i].attributes.symbol,
      "tokenUri":queryResults[i].attributes.token_uri,
    })
  }
  return results;
)};

Here is my JS code just in case

Moralis.initialize("QRgdbAtxNyOKbJCpwUsknrGdpK3T6wvJaZiu0yek");

Moralis.serverURL = "https://ycwyggz85697.usemoralis.com:2053/server";

const TOKEN_CONTRACT_ADDRESS = "0x5FbDB2315678afecb367f032d93F642f64180aa3";

init = async () => {

  hideElement(userItemsSection);

  hideElement(userInfo);

  hideElement(createItemForm);

  console.log(userUsernameField.value);

  window.web3 = await Moralis.Web3.enable();

  window.tokenContract = new web3.eth.Contract(

    tokenContractAbi,

    TOKEN_CONTRACT_ADDRESS

  );

  initUser();

  loadUserItems();

};

initUser = async () => {

  if (await Moralis.User.current()) {

    hideElement(userConnectButton);

    showElement(userProfileButton);

    showElement(openCreateItemButton);

    showElement(openUserItemsButton);

  } else {

    showElement(userConnectButton);

    hideElement(userProfileButton);

    hideElement(openCreateItemButton);

    hideElement(openUserItemsButton);

  }

};

login = async () => {

  try {

    await Moralis.Web3.authenticate();

    initUser();

  } catch (error) {

    alert(error);

  }

};

logout = async () => {

  await Moralis.User.logOut();

  hideElement(userInfo);

  initUser();

};

openUserInfo = async () => {

  user = await Moralis.User.current();

  if (user) {

    const email = user.get("email");

    if (email) {

      userEmailField.value = email;

    } else {

      userEmailField.value = "";

    }

    userUsernameField.value = user.get("username");

    const userAvatar = user.get("avatar");

    if (userAvatar) {

      userAvatarImg.src = userAvatar.url();

      showElement(userAvatarImg);

    } else {

      hideElement(userAvatarImg);

    }

    showElement(userInfo);

  } else {

    login();

  }

};

saveUserInfo = async () => {

  user.set("email", userEmailField.value);

  user.set("username", userUsernameField.value);

  if (userAvatarFile.files.length > 0) {

    const avatar = new Moralis.File("avatar.jpg", userAvatarFile.files[0]);

    user.set("avatar", avatar);

  }

  await user.save();

  alert("user info saved sucessfully");

  openUserInfo();

  console.log(userUsernameField.value);

};

createItem = async () => {

  if (createItemFile.files.length == 0) {

    alert("Please select a file");

    return;

  } else if (createItemNameField.value.length == 0) {

    alert("Please give the item a name");

    return;

  }

  const nftFile = new Moralis.File("nftFile.jpg", createItemFile.files[0]);

  await nftFile.saveIPFS();

  const nftFilePath = nftFile.ipfs();

  const nftFileHash = nftFile.hash();

  const metadata = {

    name: createItemNameField.value,

    description: createItemDescriptionField.value,

    image: nftFilePath,

  };

  const nftFileMetadataFile = new Moralis.File("metadata.json", {

    base64: btoa(JSON.stringify(metadata)),

  });

  await nftFileMetadataFile.saveIPFS();

  const nftFileMetadataFilePath = nftFile.ipfs();

  const nftFileMetadataFileHash = nftFile.hash();

  const nftId = await mintNft(nftFileMetadataFilePath);

  const Item = Moralis.Object.extend("Item");

  // Create a new instance of that class.

  const item = new Item();

  item.set("name", createItemNameField.value);

  item.set("description", createItemDescriptionField.value);

  item.set("nftFilePath", nftFilePath);

  item.set("nftFileHash", nftFileHash);

  item.set("metadataFilePath", nftFileMetadataFilePath);

  item.set("metadataFileHash", nftFileMetadataFileHash);

  item.set("nftId", nftId);

  item.set("nftContractAddress", TOKEN_CONTRACT_ADDRESS);

  await item.save();

  console.log(item);

};

mintNft = async (metadataUrl) => {

  const receipt = await tokenContract.methods

    .createItem(metadataUrl)

    .send({ from: ethereum.selectedAddress });

  console.log(receipt);

  return receipt.events.Transfer.returnValues.tokenId;

};

openUserItems = async () => {

  user = await Moralis.User.current();

  if (user) {

    showElement(userItemsSection);

  } else {

    login();

  }

};

loadUserItems = async () => {

  const OwnedItems = await Moralis.Cloud.run("getUserItems");

  console.log(OwnedItems);

};

hideElement = (element) => (element.style.display = "none");

showElement = (element) => (element.style.display = "block");

//Navbar

const userConnectButton = document.getElementById("btnConnect");

userConnectButton.onclick = login;

const userProfileButton = document.getElementById("btnUserInfo");

userProfileButton.onclick = openUserInfo;

const openCreateItemButton = document.getElementById("btnOpenCreateItem");

openCreateItemButton.onclick = () => showElement(createItemForm);

//User Profile

const userInfo = document.getElementById("userInfo");

const userUsernameField = document.getElementById("txtUsername");

const userEmailField = document.getElementById("txtEmail");

const userAvatarImg = document.getElementById("imgAvatar");

const userAvatarFile = document.getElementById("fileAvatar");

document.getElementById("btnCloseUserInfo").onclick = () =>

  hideElement(userInfo);

document.getElementById("btnLogout").onclick = logout;

document.getElementById("btnSaveUserInfo").onclick = saveUserInfo;

//Item Creation

const createItemForm = document.getElementById("createItem");

const createItemNameField = document.getElementById("txtCreateItemName");

const createItemDescriptionField = document.getElementById(

  "txtCreateItemDescription"

);

const createItemPriceField = document.getElementById("numCreateItemPrice");

const createItemStatusField = document.getElementById("selectCreateItemStatus");

const createItemFile = document.getElementById("fileCreateItemFile");

document.getElementById("btnCloseCreateItem").onclick = () =>

  hideElement(createItemForm);

document.getElementById("btnCreateItem").onclick = createItem;

//User Items

const userItemsSection = document.getElementById("userItems");

const userItems = document.getElementById("userItems");

document.getElementById("btnCloseUserItems").onclick = () =>

  hideElement(userItemsSection);

const openUserItemsButton = document.getElementById("btnMyItems");

openUserItemsButton.onlick = openUserItems;

init();

When I press "save " in Clous functions, it saves it and shows me red warning telling me to check
my logs . And here is what the logs show.


What am I doing wrong?

You can check this code: https://github.com/MoralisWeb3/youtube-tutorials/blob/5b2274126e90bdcbf06d756b09f071d89b690d19/rarible-clone/cloud_functions.js
maybe it helps

1 Like

I don’t think it can help. It gives an error that I don’t understand… That repo doesn’t have cloud functions code. Thanks anyway

I gave you the link directly to cloud_functions.js

Omg I replaced it and the error was gone! Thank yuo very much!

It still doesn’t show what it should but I guess it’s a different story :smiley: Should I delete this post?

no, you should not delete this post.

1 Like