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

2

8

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!

Wolverhampton answered 29/9, 2017 at 21:29 Comment(0)
A
7

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.

Allhallowmas answered 18/10, 2017 at 8:20 Comment(0)
I
5

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

database.tableName.where(indexId).equals(indexValue).delete();

The data type of the key is not a problem you could verify it in my example here: example

db.example.where('key').equals('one').delete();  

Maybe you are trying to delete by a property that not is an index.

Identic answered 21/10, 2017 at 21:40 Comment(1)
How can I pass dynamic value of key in equals instead hardcoding it ? Suppose I have in typescript const allItems: Todo[] = this.db.todos.toArray(); return this.db.todos.where('value').equals('wd').delete();Scalping

© 2022 - 2024 — McMap. All rights reserved.