我有以下格式的JSON对象,
$scope.indexData = {
"custid": "1",
"addresses": [
{
"addressType": "P",
"address1": ""
},
{
"addressType": "M"
}
],
"personalDetails": {
"title": "",
"name": ""
}
}
我想使用HTML5 indexDB存储对象。我如何存储对象?
我尝试了以下方式,但没有运气。
var db;
var request = window.indexedDB.open("newDatabase", 1);
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("customers");
for (var i in $scope.indexData) {
objectStore.add($scope.indexData[i]);
}
}
我收到以下错误:Uncaught DataError: Failed to execute 'add' on 'IDBObjectStore': The object store uses out-of-line keys and has no key generator and the key parameter was not provided.
答案 0 :(得分:1)
您正在指定一个指示商店使用内联键的键路径。你的代码可能是这样的:
var db;
var request = window.indexedDB.open("newDatabase", 1);
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("customers",{keyPath: "isbn"});
for (var i in $scope.indexData) {
objectStore.add({i:$scope.indexData[i], isbn:i});
}
}