Delete a record in ItemsForSale

Hi Guys, I’m trying to delete a record in the ItemsForSale table to remove it from the marketplace. I’m trying via code from the frontend. I tried several times using documentation references, but the functionality is lacking in information and I couldn’t find video.

I tried destroy and also methods used in mongo db, nothing works.
https://docs.moralis.io/moralis-server/database/objects#destroying-objects

Note: I need to pass a parameter which is the tokenId to only delete the specific record.

Thanks for the help, greetings

1 Like

Hi,

You’re getting an array back of objects, not 1 single object.
This should work, I’m using token_id & token_address but you can also use the objectId if you want:

  const query = new Moralis.Query('ItemsForSale')
  query.equalTo('token_address', item.token_address)
  query.equalTo('token_id',item.token_id)
  const object = await query.first() // just get 1 item, not array of items
   if (object) {
    object.destroy().then(() => {
      console.log('The object was deleted from ItemsForSale.');
    }, (error) => {
      console.log(error);
    });
  }

If you use it in a cloud function you will new to use object.destroy(null, {useMasterKey:true}) to override any CLP you have set on the tables.

Note that deleting a row in the database should work as well (although a sync job might put them back).

Hope that helps you out further.

1 Like

True, I hadn’t thought of that. The record is allocated in the Marketplace token memory. What do you suggest? Thx

It’s hard for me to suggest anything without knowing what you’re trying to achieve in detail.
But you can delete records from the ItemsForSale database table either from client side or cloud functions, the code is basically the same. The choice can be related to security or the way you prefer to use the api.
In cloud functions you will need to override CLP and ACL security that is set on tables using the MasterKey. Be sure to read about CLP and ACL when you plan to go towards production, because per default all tables are open to public read and write access when you start a server.
Most important, keep trying, keep learning, keep reading and never give up :slight_smile:

I see in your screenshot you want to remove an item from the marketplace. If you use a marketplace contract for that, you will need to remove the item in the contract and not the database. Removing it from the contract should send an event which can be watched by a moralis Plugin, which then runs a cloud Job that deletes or updates the item in the database.

An example from my client side code doing that:

    const removeFromMarket = () => {
      const web3 = new Moralis.Web3()
      const sender = user.value.get('ethAddress')
      marketContract.value.methods.removeItemFromMarket(props.market_id, props.token_id, props.token_address).send({from: sender})
      .on('receipt', receipt => {
        console.log('receipt:', receipt)
        // do stuff here when tx has been confirmed
      })
      .on('error', (err) => {
        console.log('error removing nft:', err)
        // do stuff with error
      })
    }

So this calls the market contract through the ABI method. The contract then emits an event that is caught by the moralis Plugin Sync and Watch Contract Events (all explained in tutorial videos).
This is my cloud function to delete the database record:

// remove items from sale ETH
Moralis.Cloud.beforeSave('EthRemovedItems', async (request) => {
  const query = new Moralis.Query('EthItemsForSale')
  query.equalTo('token_address', request.object.get('token_address'))
  query.equalTo('token_id', request.object.get('token_id'))
  const object = await query.first({useMasterKey:true})
  const logger = Moralis.Cloud.getLogger()
  if (object) {
    object.destroy(null, {useMasterKey:true}).then(() => {
      logger.info('The object was deleted from EthRemovedItems.');
    }, (error) => {
      logger.info(error);
    });
  }
})

Hope that helps :slight_smile:

2 Likes

update: you should now use object.destroy({useMasterKey:true}) instead of object.destroy(null, {useMasterKey:true})