我目前正在尝试运行 indexedDB 数据库。但是,我正在努力解决有关 indexedDB 的 put 方法的一些问题。虽然定义了keypath,并且移交的JSONObject包含一个与定义的keypath命名方式相同的值,但put方法会导致以下错误:
未捕获的 DOMException:无法在“IDBObjectStore”上执行“放置”:评估对象存储的键路径未产生值。
为了确保 JSONObject 确实包含应用作键的值,我正在记录该对象。这就是它的样子:
{"key":102019,"month":10,"year":2019,"spendings":[{"type":{"name":"Technology","importance":70,"iconURL":". /Resources/Technology.png"},"cost":"1500","name":"Macbook pro","timestamp":1571696285911}],"budget":0}
用于存储数据的代码如下:
function callbackSaveSpendingMonth(database, spendingMonth) {
let userName = defaultUserName;
let transaction = database.transaction(userName, "readwrite");
let objectStore = transaction.objectStore(userName, { keyPath: 'key' });
let JSONspendingMonth = JSON.stringify(spendingMonth);
console.log(JSONspendingMonth);
let request = objectStore.put(JSONspendingMonth);
request.onsuccess = function (event) {
console.log("The month " + spendingMonth.getMonth() + "/" + spendingMonth.getYear() + " has been saved successfully!");
}
transaction.oncomplete = function (event) {
console.log("A connection to indexedDB has successfully been established!");
}
}