Delete image from gallery by code

Hi @jonatandor35 , here’s the code. I have two problems now.

  1. Whilst testing I had placed the splice in the onReady function but for some reason it only temporarily deleted the record. When I refresh the page the record seems to reappear.

  2. I moved the action from the onReady and attempted to delete the record in the button2 onclick event, but I’m not sure how to recall the item which is stored as “index”. I read that once “var” is stored outside of a function it can be called anywhere. However, the index number generated is a response to the user selecting the image via the onItemClicked, so how can I get the index number stored so that it can be used in the export function?

  import WixData from 'Wix-data';
  
  $w.onReady (function () {
  $w("#gallery1").onItemClicked ((event)=>{
  
  var index= event.itemIndex;
  $w("#text31").text= index
  
  console.log("Gallery Item Title=" + event.item.title)
  console.log ("gallery#:", index)
  
  let image= event.item.title
  let imagecover= event.item.src
  let eventname= $w('#text28').text
 
  
  
  $w('#text30').text= image
  $w('#cover).src= imagecover
  })
  })
  
  export function button2_click(event){
  let currentItem=($w("#dynamicDataset").getCurrentItem())
  
  console.log(currentItem)
  
  currentItem.media.splice(index,1)
  console.log("item removed")
  wixData.update("Gallery", currentItem)
  
  $w("#gallery1").items= [{currentItem}]
  console.log(currentItem)
  }