- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,这是我的简单项目 js 代码示例
const deleteOneNote = (id) => {
const db = openNoteDb();
db.notes.where('id').equals(id).delete();
}
$(document).on("click", ".remove_note", function () {
const id = $(this).parent().parent().attr("id");
const db = openNoteDb();
db.notes.where('id').above(-1).modify(result => {
if (result.id == id) {
deleteOneNote(result.id);
window.location.reload(true)
}
})
})
但是我的删除功能不起作用我不明白请帮助我..
最佳答案
首先,您的函数 deleteOneNote() 必须从 db.transaction() 返回 promise ,这样您就可以等待删除完成。或者使函数异步并等待 db.transaction() 返回的 promise 。无论您选择什么,函数的调用者也必须在测试删除是否成功之前等待您的函数。
然后,我在代码上也看到了一个通病,就是Dexie实例是按需创建的,会导致内存泄漏,除非你在完成后关闭它。相反,在顶层声明 db,这样它就会在你的整个应用程序生命周期内存活,并让你的函数在任何地方都使用同一个 db 实例。
第三个常见错误(虽然可能不是这个原因)是人们在 key 存储为数字时提供字符串,反之亦然。确保提供给函数的 ID 与要删除的对象的类型不同。
// Declare one single Dexie instance in your entire application
// and use it from all functions.
const db = new Dexie("NotesDB");
// If you use modules, keep this code in a module and
// add an 'export' before 'const db = new Dexie("NotesDB");
db.version(1).stores({
notes: `
id,
time,
note,
noteBackColor,
noteTextColor,
projectType,
alarm`,
});
// Let you functions use the singleton Dexie instance:
const deleteOneNote = (id) => {
return db.transaction('rw', db.notes, function () {
// Note: Doing this in a transaction is over-kill, but ok.
return db.notes.delete(id);
}).catch((err) => {
console.log(err);
throw err; // Also, you probably want to rethrow the error...
});
}
现在,这些是来自 Dexie's best-practices section 的代码建议并且不一定是您的代码无法删除项目的原因。
一个比较有意思的事情是:
数据库中您要删除的对象的“id”属性的值是多少?我假设您的对象看起来像{id: "x", time: "y", ...}
。
你传递给函数的参数是什么?如果您要删除的对象看起来像上一个项目符号中描述的那样,则 id
参数的值必须恰好是字符串“x”——不是对象,也不是数组或任何其他类型——一个带有精确值“x”。
此代码的工作测试是:
const foo = {id: "x", time: "y"};
db.notes.put(foo).then(async () => {
if (await db.notes.get("x")) {
console.log("My object is there!");
} else {
console.log("My object is not there!");
}
console.log("Now deleting it...");
await deleteOneNote("x");
if (await db.notes.get("x")) {
console.log("My object is there!");
} else {
console.log("My object is not there!");
}
}).catch(console.error);
运行这个测试的结果应该是:
My object is there!
Now deleting it...
My object is not there!
关于javascript - Dexie.js 删除一个 id 不工作的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71876224/
我有一个使用 Dexie 包装器作为 indexedDB 的 Web 应用程序,由于某种原因,我需要无故障地重命名现有数据库,我在 Dexie 文档上找不到重命名。 最佳答案 无论是 Dexie 还是
场景: 用户在 Firefox 中打开 Dexie webapp。 用户写入一些 Dexie 数据并关闭 Firefox。 用户在 Chrome 中打开相同的网络应用。 用户能够看到之前在 Firef
所以我正在使用 dexie.js 构建一个具有离线支持的小型应用程序和 IndexedDB 第一次。如果本地计算机上已存在数据库,我想打开并使用该现有数据库 ->因此我使用 Dexie.exists(
谁能告诉我当我调用 dexie.js 的 count() 函数时会发生什么: TypeError: 'caller', 'callee', and 'arguments' properties may
以下查询抛出错误: DB.classes .where("name") .anyOf(["John", "Rick"]) .or("nickname") .anyO
我正在使用 Dexie(IndexedDb 库)。我正在经历the API看看是否有办法选择两个日期之间的事件,我看到有一个 . Between() 方法以这种方式使用: table.where(in
我在 Dexie 中运行事务时收到此错误: name: SubTransactionError message: table _ not included in parent transaction
我们制作了一个开源浏览器。 请更正选项卡的加载项。有必要将选项卡添加到开头。现在选项卡添加到末尾: var newTab = tabs.add({}, tabs.getIndex(tabs.getSe
我正在使用Dexie.js有时我在尝试访问数据库时收到错误“数据库打开时无法添加版本”。 是否有处理此错误的最佳实践?在这种情况下我需要关闭数据库并尝试重新打开它吗? 我的代码是这样的(用 Coffe
我正在制作离线和在线工作的 Notes PWA 应用程序。 我正在使用 Dexie.js 在 IndexedDB 上保存笔记记录.我正在使用 Quasar作为前端和后端 Laravel 8 . 我想将
我想做一些类似的事情,myTable.update({ location: 'Paris'}, { location: '' } 并让 myTable 中的所有对象将“Paris”的 location
我正在使用 Dexie 访问闪存卡制造商项目中的 IndexedDB。我可以按预期操作数据库,如果我关闭并重新打开浏览器,它会保留在数据库中,但通常当我几天不处理项目后打开项目时,我放入数据库中的存储
来自 this question我可以在我的表中找到所有对象,其中值出现在作为数组的字段中。现在,我需要为该表中的所有对象从该字段中删除该值。 例如,假设一个 events表,有对象: { peopl
我正在使用 Dexie DB 版本 2.0.4 以及 Angular 8 和 Electron。我意识到,我在数据库中输入的数据越多,对其进行的查询越多,我的数据库调用就会变得越慢。 我是一名 Dex
我有一个表,其中每个对象都有一个字符串数组字段:例如,{ people: ['John', 'Bob', 'Sue'] }。我需要表中 people 数组中包含“Sue”的所有对象。 德克西能做到吗?
我的基本 dexie 数据库方案是这样的。 const db = new Dexie('MyDatabase'); // Declare tables, IDs and indexes db.vers
TL;DR:collection.modify() 可处理几条记录,但在尝试一次修改超过 10 条记录时失败。为什么? 我有一些代码可以使用 Javascript、HTML 和 IndexedDB 上
我正在使用一个名为 bugout 的库(这是一个基于 webtorrent 构建的 API)来创建一个 P2P 房间,但我需要它来根据使用 Dexie 查找表的值来创建房间。 我知道这已在 Stack
我正在使用 startsWithIgnoreCase 查询 dexie 数据库并将结果推送到一个数组中,但是在打印或使用它时,它会抛出一个未定义的错误 我已经尝试使用 JSON.stringify、t
我正在使用dexie.js ,它是一个indexDB 包装器。无论如何,我有一个从用户本地存储调用的数组,我的函数应该迭代数据库中的每个列表项并显示它。但是,单击我的 Butane 后,它仅显示最近输
我是一名优秀的程序员,十分优秀!