2016-01-18 55 views
2

多個對象我在下面格式JSON對象,存儲使用HTML5 INDEXDB

$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.

回答

1

你所指定的指示商店使用在線密鑰的的keyPath。你的代碼也許是這樣的:

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});  
     } 

} 
+0

感謝#Vino..its工作正常 – vishnu