更新 indexeddb 中的单个字段

Phi*_*enn 0 indexeddb

以下工作代码用于设置我的问题:

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})
	}
	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)
	}
}
Run Code Online (Sandbox Code Playgroud)

问:我怎么说相当于:

update myData set field2='c' where key=1
Run Code Online (Sandbox Code Playgroud)

我是否必须在 .put 语句中同时提供 field1 和 field2?

Jos*_*ell 5

是的,你必须把完整的对象,例如

myObjectStore.get(1).onsuccess = function(e) {
  var obj = e.target.result;
  obj.field2 = 'c';
  myObjectStore.put(obj, 1);
};
Run Code Online (Sandbox Code Playgroud)