Delete specific row after wixData.query

Hello! :sweat_smile:
How can I delete a row in a database specifically from the current user after wixData.query?

wixData.query("ReviewsPermission")
.eq("_owner", wixUsers.currentUser.id)
.find()
.then((result)=>{
 let data = result.items;
 let ownerID = data._owner;
 // I don't know what's next...
})

I believe this is basic, but I don’t know the specific code for this.

Thank you in advance!

Your question is unclear. No one can understand what you wish to delete and from where.

First of all I need to have a query in ReviewsPermission database to find the row which have the current user’s _owner. Then after finding it, the code should delete that row which means to say all of the items that were inserted there will be removed or deleted. So if there is a total of 10 rows in ReviewsPermission database, after deletion, it will be 9 rows.

@aeroengineerz7 Here is the code

import wixUsers from 'wix-users';
import wixData from'wix-data';

let user = wixUsers.currentUser;
let userId = user.id;           


wixData.query("ReviewsPermission")
.eq("_owner", userId)
.find()
.then((result)=>{
 let ownerId = result.items[0]._owner;
})
wixData.remove("ReviewsPermission", ownerId)
 .then( (results) => {
    let item = results; 
 } );

Post if it works !!!

@ajithkrr Still didn’t work… :disappointed_relieved:

At first, I tried to copy your code but there’s an error in this part of the code.

wixData.remove("ReviewsPermission", ownerId)

I noticed that ownerId was undefined, and so I tried to make it this way.

wixData.query("ReviewsPermission")
.eq("_owner", wixUsers.currentUser.id)
.find()
.then((result)=>{
 let ownerId = result.items[0]._owner;
 wixData.remove("ReviewsPermission", ownerId)
})

And still didn’t work.

@aeroengineerz7

wixData.remove("ReviewsPermission", ownerId)
  .then( (results) => {
    let item = results; 
  } )

@ajithkrr
I already did that, but still didn’t remove the whole row in my database

wixData.query("ReviewsPermission")
 .eq("_owner", wixUsers.currentUser.id)
 .find()
 .then((result) => {
 let ownerId = result.items[0]._owner;
 
   wixData.remove("ReviewsPermission", reviewsId)
   .then((results) => {
     let item = results;
   });
})

@aeroengineerz7 Hey Juan !

I think you are doing wrong, this line →

wixData.remove("ReviewsPermission", reviewsId)

It should be like this →

wixData.remove("ReviewsPermission", ownerId)

Because after the query, you are assigning the result as ownerId on this line →

.then((result) => {
 let ownerId = result.items[0]._owner;

So when you remove that row that should be →

wixData.remove("ReviewsPermission", ownerId)

Ajith

@ajithkrr
Sorry…:sweat_smile: I input the wrong code here, but the code in my website is this which still didn’t work

wixData.query("ReviewsPermission")
 .eq("_owner", wixUsers.currentUser.id)
 .find()
 .then((result) => {
 let ownerId = result.items[0]._owner;
 
   wixData.remove("ReviewsPermission", ownerId)
   .then((results) => {
     let item = results;
   });
})

May I know if you have already tried this kind of code before?

@aeroengineerz7 Instead of this →

let ownerId = result.items[0]._owner;

Try this

let ownerId = result.items[0]._id;
wixData.query("ReviewsPermission")
 .eq("_owner", wixUsers.currentUser.id)
 .find()
 .then((result) => {
 let ownerId = result.items[0]._id;
 
   wixData.remove("ReviewsPermission", ownerId)
   .then((results) => {
     let item = results;
   });
})

@ajithkrr Still didn’t work :disappointed_relieved:

@aeroengineerz7 Juan can you provide your whole code . The problem could be there

@ajithkrr

export function deleteRow_click(event) {
 wixData.query("ReviewsPermission")
 .eq("_owner", wixUsers.currentUser.id)
 .find()
 .then( (result) => {
   let ownerId = result.items[0]._id;
 
   wixData.remove("ReviewsPermission", ownerId)
   .then( (results) => {
     let item = results;
   });
  })
  .catch ((err)=>{
    let errorMsg = err;
  })
}

@aeroengineerz7
Hi Juan


  wixData.query("ReviewsPermission")
 .eq("_owner", userId)
 .find()
 .then( (result) => {
 if (result.items.length > 0) {
 let ownerId = result.items[0]._id;
 
   wixData.remove("ReviewsPermission", ownerId)
   .then( (results) => {
 let item = results;
   console.log("good");
   })
   .catch ((err)=>{
 let errorMsg = err;
    console.log(errorMsg);
  }); 
}
  else { 
     console.log("nothing");
   }
  })
  .catch ((err)=>{
 let errorMsg = err;
    console.log(errorMsg);
  }); 

Copy this code and go to preview.
Click the respective button.
Check the console and see what you get !!

YES! I already understand the code and it works! Thanks @ajithkrr !

This is for anyone of you who still doesn’t know how to remove a row in your database using the code below.

wixData.remove("CollectionName", _id)

The _id in that code is literally the _id in your database NOT the _owner nor any field key in your database because through the use of the _id, it can easily find which row should be deleted. So it’s up to you how you will put the specific _id in that code.

Well, done. This is the real learning process! Keep it like that :wink:

Well done. This is the real learning process!. Keep it like that :wink: