【发布时间】:2021-10-13 00:14:42
【问题描述】:
我正在尝试将条目从 mysql 数据库(由 PHP 通过 JSON 提供)传输到 localforage 数据库。 transactions 下列出的记录使用 id 属性编号(在 JSON 字符串中从 1 升序到大约 400)。我的目标是使用这些 id 作为 localforage 的键。然而,结果如下面的 DevTools 截图所示。
var tmp, id;
var json = JSON.parse(data); // data is the JSON encoded string returned from server
for(var i = 0; i < json.transactions.length; i++){
tmp = json.transactions[i];
id = String(tmp.id);
console.log(typeof id);
delete tmp.id;
transactions.setItem(id, tmp).then(function (value) {
//console.log(value);
}).catch(function (err) {
console.log(err);
});
}
resulting database in localforage in DevTools (Screenshot)
我怎样才能让 localforage 使用正确的 id(1、2、3、4 ... 400 等)而不是这些转换后的键(似乎来自另一个数字系统)?
【问题讨论】:
-
不要在这样的for循环中编写回调代码,例如参见stackoverflow.com/questions/750486中提到的问题
标签: key indexeddb data-conversion localforage