How to delete both relevant DB at the same time using wix code

Hello everyone

How to write code for beginner to delete both Products database relevant to ProductImage database at the same time by using icon button in repeater?

Thank you for your help
RGDS, Chris

Hello Chris

You can delete by adding button click event. on that event you can delete the item using remove() function from both collections.

To use the remove method you need to know the item id (for both collections), and to get the id you need the item object, to get the item object you can use getCurrentItem() Function.

However, note that you might need to query the database to get the id (in case the collection you want to remove from is different than the one connected to the Repeater). in this case you get the image title from the itemObj and you query both collections to get the ids.

Here’s a code example of how to implement that:

import wixData from 'wix-data';

// ...
 
export function deleteBtn_click(event) {

// get the current item 
let itemObj = $(#myDataset).getCurrentItem()

// get the object id if the collection to delete from is the same as the one connected to the repeater
let itemId1 = itemObj._id

// get the object id if the collection to delete from is different from the one connected to the repeater
 let itemName = itemObj.name
 let itemId2;
 wixData.query("productImageDB")
 .eq("productName",itemName)
 .find()
 .then((results) =>{
   itemId2=results.items[0]._id
 })

//delete from both collections
wixData.remove("productDB", "itemId1")
  .then( (results) => {
    let item = results; //see item below
  } )
wixData.remove("productImageDB", "itemId2")
  .then( (results) => {
    let item = results; //see item below
  } )

}

Good Luck!
Massa

Hi,

My suggestion is to add an onItemReady() event handler to the repeater and inside the handler add an onClick() event to the delete button.
(I add a button to the repeater’s items)
Inside the event, use Query() function in order to find the id of the specific item you are trying to delete and then use Remove() function.

$w.onReady( function () {

  $w("#dataset").onReady(() => {
    $w('#repeater').onItemReady(($item, itemData, index) => {
        $item('#button').onClick(async() => {
      let results1 = await wixData.query("Products").eq("title", 
                     $item('#text1').text).find()             
            
      let results2 = await wixData.query("ProductsImage").eq("title",
                $item('#text1').text).find()   
                 
                wixData.remove("Products", results1.items[0]._id)
                results2.items.forEach(function (element) {
                wixData.remove("ProductsImage", element._id)

                });
            });
        });
    });
});

At the code- $item(‘#text1’).text is the current value of the element ‘#text1’ from the repeater that is being used in the Query() function in order to find the specific item .

Here you can read more about Query() function in database.
Here you can read more about Remove() function in database.
Here you can read more about onItemReady() function.

Best of luck!
Sapir

1 Like

Hi, Messa

Nothing happened when I clicked delete button. What shall I do?


export function delete_click(event, $w) {

let itemObj = $w(“#dataset1”).getCurrentItem();
let itemId1 = itemObj._id
let itemName = itemObj.name
let itemId2;
wixData.query(“productImage”)
.eq(“productName”, itemName)
.find()
.then((results) => {
itemId2 = results.items[0]._id;
});

wixData.remove("products", "itemId1") 
    .then((results) => { 

let item = results;
});
wixData.remove(“productImage”, “itemId2”)
.then((results) => {
let item = results;
})

}


Thank you for your precious time.

Rgds,
Chris

Hi, Sapir
Thank you very much for your quick reply.
I have no idea what I’m doing wrong the code is still not working.
Can you please take a look at my site

What do I replace “newData” and “urlPages” by?

Thanks for your attention. I’m looking forward to your reply.
Chris

@perfect-filming What is the name of the page?
and you need to write the name of your databases in the Remove() functions.

@sapirh Page : Admin Products

Here is my code :

$w.onReady( function () {

$w(“#dataset1”).onReady(() => {
$w(‘#repeaterProduct’).onItemReady(($item, itemData, index) => {
$item(‘#delete’).onClick( async () => {
let results1 = await wixData.query(“Products”).eq(“name”,
$item(‘#txtname’).text).find()

let results2 = await wixData.query(“ProductsImage”).eq(“productName”,
$item(‘#txtname’).text).find()

            wixData.remove("Products", results1.items[0]._id) 
            results2.items.forEach( **function**  (element) { 
            wixData.remove("ProductImage", element._id) 

            }); 
        }); 
    }); 
}); 

});

Thanks
Chris

Hi, Sapir

Thank you so much It works!

Have a wonderful year
Best Regards,
Chris