首页 > 解决方案 > localForage 迭代不适用于 JS 文件

问题描述

数据库中有 6 个键,我通过在浏览器控制台中检查它来确认它。

下面的代码只循环了 3 次,条件变为 false 后,它就停止了。

localforage.iterate(function (value, key, iterationNumber) {
    console.log(key);
    if (key != lastReminderIdKey) {                            
        //console.log(value);                            
    }
}).then(function (value) {
    console.log('Iteration has completed');
}).catch(function (err) {
    console.log(err);
});

该代码在 HTML 文件中时运行良好。然后我将它移到一个 JS 文件中,迭代/循环现在只部分工作。当我将它移回 HTML 文件时,它工作正常。

DB 中的键是1, 2, lastReminderKey, 3, 4, 5. 在控制台日志中,当从 JS 文件运行时,它会打印:

1
2
lastReminderKey
Iteration has completed

这种行为的原因可能是什么?

标签: javascriptindexeddblocalforage

解决方案


您可能在 2 个地方使用不同的 localforage 配置。尝试验证两个 localforage 实例是否具有完全相同的namestoreNamedriver.

您可以在选项对象上检查这些属性。

const options = localforage.config();

推荐阅读