简体   繁体   English

Dexie JS..删除问题

[英]Dexie JS .. Delete issue

I am trying to delete a record by ID from indexedDB using dexie.js.我正在尝试使用 dexie.js 从 indexedDB 中按 ID 删除记录。 (Dexie.js is a wrapper for indexedDB) (Dexie.js 是 indexedDB 的包装器)

//Database and table structure
window.trollflixDB_offline_jokes = new Dexie("trollflixDB_offline_jokes");
trollflixDB_offline_jokes.version(1).stores({
    offline_jokes: "++id, joke_id, joke"
});

//Get data
window.get_offline_jokes = function(){
    trollflixDB_offline_jokes.offline_jokes.where('id').above(-1).limit(1).each(result => {
        console.log(result.id); //122
        del_joke(result.id);
    });
};

//Delete data
window.del_joke = function(delete_id){
    console.log(delete_id); //122
    console.log(typeof delete_id, typeof Dexie, typeof trollflixDB_offline_jokes, typeof trollflixDB_offline_jokes.offline_jokes);
    //number function object object

    trollflixDB_offline_jokes.offline_jokes.where('id').equals(delete_id).delete();
};

get_offline_jokes();

The function is working when I am trying it on DEV console or out of the function scopes.当我在 DEV 控制台上或在 function 范围外尝试时,function 正在工作。 trollflixDB_offline_jokes.offline_jokes.where('id').equals(122).delete();

I tried to use different syntax, and it does not work.我尝试使用不同的语法,但它不起作用。 trollflixDB.recent_notifications.where({'id': delete_id}).delete();

Tested on Opera, Chrome.在 Opera、Chrome 上测试。 There is no any Errors, Warnings, or Promise Exceptions returned.没有返回任何错误、警告或 Promise 异常。

If you want to TEST this on your browser, here is how to put the data quickly with copy-paste to dev console...如果您想在浏览器上测试它,这里是如何通过复制粘贴快速将数据放入开发控制台...

const getScript=e=>new Promise((o,n)=>{const t=document.createElement("script");t.src=e,t.async=,0.t,onerror=ntonload=t.onreadystatechange=function(){const e=this;readyState.e&&"loaded".==e&&"complete",==e||(t,onload=t.onreadystatechange=null.o())};document:head.appendChild(t)}).getScript("https.//unpkg.com/dexie@2.0.4/dist/dexie.min,js").then(()=>{window.trollflixDB_offline_jokes=new Dexie("trollflixDB_offline_jokes"):trollflixDB_offline_jokes,version(1),stores({offline_jokes,"++id. joke_id. joke"}):trollflixDB_offline_jokes,offline_jokes:put({joke_id;"32HUHU)",joke:"xD XD"})});

Please let your promise-using functions return a promise so you can chain the calls to them.请让您使用承诺的函数返回 promise,以便您可以将调用链接到它们。

Regarding calling delete within Collection.each(), it is not possible because Collection.each() is readonly unless you call it from a readwrite transaction.关于在 Collection.each() 中调用 delete,这是不可能的,因为 Collection.each() 是只读的,除非您从读写事务中调用它。 If you want to modify things within the iteration, use Collection.modify() instead of Collection.each().如果您想在迭代中修改内容,请使用 Collection.modify() 而不是 Collection.each()。 This is described in the Notes section for Collection.each: https://dexie.org/docs/Collection/Collection.each()#notes这在 Collection.each 的注释部分中进行了描述: https://dexie.org/docs/Collection/Collection.each()#notes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM