indexedDB错误记录

DOMException: Failed to execute ‘put’ on ‘IDBObjectStore’: Evaluating the object store’s key path did not yield a value.

通过查找发现出现这种错误的原因是keypath在保存的数据中没有定义,在保存的数据中需要添加keypath对象。

IndexedDB中,一次性读取所有数据通常不是最佳实践,因为IndexedDB设计为大型数据集,并且不适合一次性加载所有数据。不过,如果你确实需要这样做,下面是一个基本的示例,展示如何遍历整个对象存储(Object Store)并读取所有条目: ```javascript const request = indexedDB.open('yourDatabaseName', yourDatabaseVersion); request.onsuccess = function(event) { const db = event.target.result; const transaction = db.transaction(['yourObjectStoreName'], 'readonly'); const objectStore = transaction.objectStore('yourObjectStoreName'); let cursorRequest = objectStore.openCursor(); cursorRequest.onsuccess = function(event) { const cursor = event.target.result; if (cursor) { // 已找到一条记录,获取数据 const data = cursor.value; console.log(data); // 关闭当前记录的游标 cursor.continue(); } else { // 所有记录都已读取完成 console.log("No more records to read."); } }; cursorRequest.onerror = function(errorEvent) { console.error("Error reading records:", errorEvent.target.error); }; }; // 错误处理 request.onerror = function(event) { console.error("Error opening database:", event.target.error); }; ``` 在这个例子中,我们首先打开数据库,然后在一个只读事务中打开对象存储。接着使用`openCursor()`方法开始遍历。每次`onsuccess`回调会检查是否存在下一个游标(即记录),如果有就取出数据,然后继续直到没有更多的记录。 请注意,这种方法对于大数据量的表可能会消耗大量资源,并且如果数据增长,性能会迅速下降。通常更推荐分页或基于需求的懒加载策略。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunqy1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值