【问题标题】:IndexedDB update by idIndexedDB 按 id 更新
【发布时间】:2013-02-10 17:01:17
【问题描述】:

我正在尝试更新我的 indexedDB 记录,但出现此错误

DataError:提供给操作的数据不符合要求。 源文件

我已经尝试过this,但没有成功

这是我的功能:

function updNotes(text, timestamp, blob)
{
var obj = {text: text, timestamp: timestamp};
if (typeof blob != 'undefined')
obj.image = blob;
store = getObjectStore("notes", 'readwrite');
objKeyRange = IDBKeyRange.only(+objtoedit);
req = store.openCursor(objKeyRange);
req.onsuccess = function(evt){
  var cursor = evt.target.result;
  console.log(cursor.key);
  //do the update
  var objRequest = cursor.update(obj);
  objRequest.onsuccess = function(ev){
    console.log('Success');
    };
  objRequest.onerror = function(ev){
    console.log('Error');
    };
  };
  req.onerror = function(evt){
    console.log('Error');
  };

谁能帮我解决这个问题?

最好的问候

【问题讨论】:

  • 您为什么不使用 PUT api 的任何原因?

标签: indexeddb


【解决方案1】:

我知道如何做到这一点。 这个功能解决了我的问题。

function updNotes(id, text, timestamp, blob)
{
console.log(id);
store = getObjectStore("notes", 'readwrite');
req = store.get(+id);
req.onsuccess = function(evt){
  var data = evt.target.result;
  data.text = text;
  data.timestamp = timestamp;
  if (typeof blob != 'undefined')
  data.image = blob;
  //do the update
  var objRequest = store.put(data);
  objRequest.onsuccess = function(ev){
    console.log('Success in updating record');
    };
  objRequest.onerror = function(ev){
    console.log('Error in updating record');
    };
  };
  req.onerror = function(evt){
    console.log('Error in retrieving record');
  };


}

【讨论】:

  • 那么你使用了PUT?就像我评论的那样? ;)
猜你喜欢
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 2022-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多