【问题标题】:Dexie.js - table.delete(id) not working for per-row deletionDexie.js - table.delete(id) 不适用于每行删除
【发布时间】:2018-03-11 20:23:27
【问题描述】:

我刚从 Dexie 开始,我似乎无法摆脱困境。

我有一个小型数据库(少于 1000 行),一旦我知道该行已发送到远程 API,我会尝试逐一删除每一行。 我也可以成功保存到表中(由ID和存储序列化对象的列定义)

这是我的代码:

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
    })
}

除了最后的删除语句之外的所有这些。 关于我应该如何解决这个问题的任何想法?对我来说重要的是逐行删除。

提前致谢!

【问题讨论】:

    标签: dexie


    【解决方案1】:

    我遇到了同样的问题,Eugenia Pais 的回答对我不起作用。所以经过一些测试,我发现问题出在变量的类型上:我使用的是字符串,但需要一个数字,所以我是这样解决的:

    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);
    });
    

    因此请注意,您使用的是数字作为参数。

    【讨论】:

      【解决方案2】:

      密钥的数据类型不是问题,您可以在我的示例中验证它:example

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

      也许你正试图通过一个不是索引的属性来删除。

      希望对你有帮助!

      【讨论】:

      • 如何在 equals 中传递键的动态值而不是硬编码?假设我有打字稿const allItems: Todo[] = this.db.todos.toArray(); return this.db.todos.where('value').equals('wd').delete();
      【解决方案3】:

      删除每一行的正确方法应该是选择特定的行并删除它:

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

      【讨论】:

      • 如何在 equals 中传递 key 的动态值,而不是对其进行硬编码?假设我有打字稿const allItems: Todo[] = this.db.todos.toArray(); return this.db.todos.where('value').equals('wd').delete();
      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多