Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dexie.js - table.delete(id) not working for per-row deletion

Tags:

dexie

i'm just starting out with Dexie, and I seem to be coming unstuck.

I have a small database (less than 1000 rows), and i'm trying to delete each row one-by-one once I know that the row has been sent to a remote API. I can also successfully save to the table (which is defined by an ID and a column storing a serialised object)

here's my code:

if (online) {
    //we query the db and send each event
    database.open()
    let allEvents = database.events.toCollection()
    let total = allEvents.count(function (count) {
        console.log(count + ' events in total')
        //a simple test to ensure we're seeing the right number of records
    })
    allEvents.each(function(thisEvent){
        //push to remote API
        console.log('deleting ' + thisEvent.id)
        database.events.delete(thisEvent.id) //<= this doesn't seem to be working
    })
}

All of this with the exception of the final delete statement. Any ideas on how I should fix this? the important thing for me is to delete on a per-row basis.

thanks in advance!

like image 964
AndrewO Avatar asked Sep 29 '17 21:09

AndrewO


2 Answers

I was experiencing the same problem, and the answer from Eugenia Pais wasn't working for me. So after some tests, I saw the trouble was with the type of the variable: I was using a string, but a number is needed, so this is how I solved it:

function removeRow (primaryKey) {
  primaryKey = parseInt(primaryKey);
  databaseName.tableName.where('primaryKey').equals(primaryKey).delete().then(function(deleteCount) {
    console.log ("Deleted " + deleteCount + " rows");
  }).catch(function(error) {
    console.error ("Error: " + error);
});

So be aware you are using a number as argument.

like image 152
siderio2 Avatar answered Nov 18 '22 15:11

siderio2


The correct way to delete each row should be selecting the specific row and delete it:

database.tableName.where(indexId).equals(indexValue).delete();
like image 27
Eugenia Pais Avatar answered Nov 18 '22 17:11

Eugenia Pais