我刚刚开始使用 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
})
}
Run Code Online (Sandbox Code Playgroud)
除了最后的 delete 语句之外,所有这些都是如此。关于我应该如何解决这个问题的任何想法?对我来说重要的是逐行删除。
提前致谢!
小智 7
我遇到了同样的问题,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);
});
Run Code Online (Sandbox Code Playgroud)
因此请注意您正在使用数字作为参数。
小智 5
删除每一行的正确方法应该是选择特定行并将其删除:
database.tableName.where(indexId).equals(indexValue).delete();
Run Code Online (Sandbox Code Playgroud)
密钥的数据类型不是问题,您可以在我的示例中验证它:示例
db.example.where('key').equals('one').delete();
Run Code Online (Sandbox Code Playgroud)
也许您正在尝试通过不是索引的属性进行删除。
| 归档时间: |
|
| 查看次数: |
2953 次 |
| 最近记录: |