function setup() {
myRequest = indexedDB.deleteDatabase('myDatabase')
myRequest.onsuccess = function() {
var myRequest
myRequest = indexedDB.open('myDatabase')
myRequest.onupgradeneeded = function(response) {
var myDatabase
,myObjectStore
myDatabase = response.target.result
myObjectStore = myDatabase.createObjectStore('myData',{autoIncrement:true})
myObjectStore.createIndex('myIndex', 'field2')
}
myRequest.onerror = function(response) {
debugger
}
myRequest.onsuccess = function(response) {
var myTransaction
,myObjectStore
,myRequest
,obj = {}
window.myDatabase = response.target.result
myTransaction = myDatabase.transaction(['myData'],'readwrite')
myObjectStore = myTransaction.objectStore('myData')
obj.field1 = 'a'
obj.field2 = 'b'
myObjectStore.add(obj)
obj = {}
obj.field1 = 'c'
myObjectStore.add(obj)
}
}
}
setup()
function myFunction() {
var myTransaction = myDatabase.transaction(['myData'])
var myObjectStore = myTransaction.objectStore('myData')
var myIndex = myObjectStore.index('myIndex')
// The following line is where I need help:
var myRange = IDBKeyRange.only(null)
// I'm trying to get the objects where field2 doesn't exist.
var myRequest = myIndex.openCursor(myRange)
myRequest.onsuccess = function(response) {
result = response.target.result
if (result) {
console.log(result)
result.continue()
}
}
}
</script>
<a href="javaScript:myFunction();">click here</a>
答案 0 :(得分:2)
你不能以直截了当的方式做到这一点。无法查询丢失/空字段。
但是,你可以聪明并解决这个问题。