Cannot change sold flag

Hi,

When a NFT is sold, the cloud function beforeSave cannot alter the value (boolean) of column NFTForSale in the NFTForSale table, even if I try manually change it to true in the dashboard, the value always immediately resets to false.

Before NFTForSale Cloud Function:

Moralis.Cloud.beforeSave("NFTForSale", async (request) => {
  request.object.set('isSold', false);
  request.object.set('isCanceled', false);
  
  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("accounts", owner);
    const userObject = await userQuery.first({useMasterKey:true});
    if (userObject) {
      request.object.set('user', userObject);
    }
    request.object.set('token', object);
  }

  const nftQuery = new Moralis.Query("NFTs");
  nftQuery.equalTo("token_id", request.object.get('tokenId'));
  const nftObject = await nftQuery.first({useMasterKey:true});

  if (nftObject) {
    nftObject.set('put_on_sale_processing', false);
    await nftObject.save();
  }
});

Before NFTSold Cloud Function:

Moralis.Cloud.beforeSave("NFTSold", async (request) => {
  const query = new Moralis.Query("NFTForSale");
  query.equalTo("tokenAddress", request.object.get('tokenAddress'));
  query.equalTo("tokenId", request.object.get('tokenId'));
  query.equalTo("isSold", false);
  query.equalTo("isCanceled", false);
  const item = await query.first();

  if(item) {
    item.set('isSold', true);
    await item.save();

    const buyerQuery = new Moralis.Query(Moralis.User);
    buyerQuery.equalTo("accounts", request.object.get('buyer'));
    const buyerObject = await buyerQuery.first({useMasterKey:true});
    if (buyerObject) {
      request.object.set('buyer', buyerObject);

      const sellerQuery = new Moralis.Query(Moralis.User);
      sellerQuery.equalTo("accounts", request.object.get('seller'));
      const sellerObject = await sellerQuery.first({useMasterKey:true});
      if(sellerObject) {
        request.object.set('seller', sellerObject);
      }
    
      const queryNFT = new Moralis.Query("NFTs");
      queryNFT.equalTo("token_address", request.object.get('tokenAddress'));
      queryNFT.equalTo("token_id", request.object.get('tokenId'));
      const nftObject = await queryNFT.first({useMasterKey:true});
    
      if(nftObject) {
        nftObject.set("sale_processing", false);
        nftObject.set("owner", buyerObject);
        await nftObject.save();
      }
    }
    request.object.set('token', tokenObj);
  }
});

Does anyone know what could be possibly causing this?

Thank you, any help is appreciated.

It sounds like there is some code that sets it to false automatically.

“even if I try manually change it to true in the dashboard”…seems a Moralis service issue.

if it is set to false in before save, I don’t know how you can update it

does the “beforeSave” triggers every time an item is updated (even via Dashboard) ? or is only triggered when a blockchain event is emitted ?

Regards

it also triggers when you make an update in dashboard

Ok, thank you for the clarification.

Regards