Before save triggers for "soldItems" errors

I’ve been continuing “I Cloned Rarible in 24H” series part 12. then I got an issue, my item in itemsForSale didn’t get uid.

here is the marketplace contract snippet code.

event itemAdded(uint256 id, uint256 tokenId, address tokenAddress, uint256 askingPrice);
event itemSold(uint256 id, address buyer, uint256 askingPrice);

function buyItem(uint256 id) payable external ItemExist(id) IsForSale(id) HasTransferApproval(itemsForSale[id].tokenAddress, itemsForSale[id].tokenId) {
        require(msg.value >= itemsForSale[id].askingPrice, "Not enough funds sent!");
        require(msg.sender != itemsForSale[id].seller);

        itemsForSale[id].isSold = true;
        activeItems[itemsForSale[id].tokenAddress][itemsForSale[id].tokenId] = false;
        IERC721(itemsForSale[id].tokenAddress).safeTransferFrom(itemsForSale[id].seller, msg.sender, itemsForSale[id].tokenId);

        emit itemSold(id, msg.sender, itemsForSale[id].askingPrice);

so when I query soldItems, const item isn’t detected.

here is the cloud functions full code.

Moralis.Cloud.beforeSave("itemsForSale", async (request) => {
  const query = new Moralis.Query("EthNFTOwners");
  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("account", owner);
    const userObject = await userQuery.first(useMasterKey:true);
    if (userObject){
      request.object.set("user", userObject);
    request.object.set("token", object);

Moralis.Cloud.beforeSave("soldItems", async (request) => {
  const query = new Moralis.Query("itemsForSale");
  query.equalTo("uid", request.object.get("uid");
  const item = await query.first();
  if (item){
    request.object.set("item", item);
    item.set("isSold", true);

    const userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("account", request.object.get("buyer"));
    const userObject = await userQuery.first(useMasterKey:true);
    if (userObject){
      request.object.set("user", userObject);

I change to the second user and add new items into itemsForSale, they get uid. but the issue is still there.


Hey @raihansulaiman

You have a typo: useMasterKey:true should be in {}: userQuery.first({useMasterKey:true})

All save functions should be like:, {useMasterKey:true})

It depends on CLP settings, but usually you need to use methods below with masterkey:

1 Like

thanks, btw do you have any advice for “soldItems” function? “const item = await query.first()” doesn’t work properly like “const object = await query.first()” in “itemsForSale” function

Can you paste the latest version of the code? As I can see lines that looks like query.equalTo("uid", request.object.get("uid"); and should be query.equalTo("uid", request.object.get("uid"));

1 Like

Hahaha my bad, it works now, thanks

1 Like