更新indexedDB [英] Update indexedDB

查看:222
本文介绍了更新indexedDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我想更新记录indexdb。我想实现一个通用的方法,允许我升级到任何字段。

为此,我有一个函数返回我需要的记录。

  function getObjectStoreClienteDB(clave,valor){
// Ahora recogeremos los datos de nuestroalmacén& quot; Productos& quot;
var transaction = db.transaction([customers],readwrite);
var objectStore = transaction.objectStore(customers);

var index = objectStore.index(clave);
var singleKeyRange = IDBKeyRange.only(valor);

//若要使用其中一个键范围,请将其作为openCursor()/ openKeyCursor()的第一个参数传递给
$ b $返回index.openCursor(singleKeyRange);
}

另一个更新返回的记录

 函数updateClienteDB(clave,valor,newvalor){
console.log(updateClienteDB ... clave:+ clave +valor:+ valor + newvalor:+ newvalor);
var objectStore = db.transaction([customers],readwrite)。objectStore(customers);

request = getObjectStoreClienteDB(name,valor);

request.onsuccess = function(event){
//获取我们要更新的旧值
var data = request.result;

//更新对象中要更改的值
if(clave ==name)
data.name = newvalor;
else if(clave ==email)
data.email = newvalor;
else if(clave ==matricula)
data.matricula = newvalor;
else if(clave ==telefono)
data.telefono = newvalor;

//把这个更新的对象放回到数据库中。
var requestUpdate = objectStore.put(data);
requestUpdate.onerror = function(event){
console.log(addCliente ...+ name ++ email ++ event.target.errorCode);
};
requestUpdate.onsuccess = function(event){
console.log(All done!);
};
};


var requestUpdate = objectStore.put(data );

错误:
未捕获TransactionInactiveError:未能在'IDBObjectStore'上执行'put':交易已经完成。

解决方案

尝试使用相同的readwrite事务。它看起来像你在两个地方调用db.transaction(...)。然后,您尝试在不同事务的上下文中更新链接到其他事务的对象存储。结果是其中一个事务完成(其生命期结束),可能是因为它超时,因为它没有检测到任何已注册的请求。 objectStore变量处于事务完成的时间内,所以你得到一个事务完成错误。



这个问题很简单。您只需要在生命周期结束之前在同一个事务中注册新的请求(放置请求)。要做到这一点,你可以(a)内联语句 request = getObjectStoreClienteDB(name,valor); ,以便将其附加到相同的事务实例或b)将交易实例存储在一个单独的变量中,并在两个调用中引用它。



例如,下面的代码是使用单个事务的修改: / p>

  //这个函数被改变了,它现在将一个活动的事务作为它的第一个参数。 

function getObjectStoreClienteDB(transaction,clave,valor){
// Ahora recogeremos los datos de nuestroalmacén& quot; Productos& quot;

//这被改变了。而不是创建一个新的事务,我们只是引用传递给这个函数的
//事务实例
var objectStore = transaction.objectStore(customers);

var index = objectStore.index(clave);
var singleKeyRange = IDBKeyRange.only(valor);

//若要使用其中一个键范围,请将其作为openCursor()/ openKeyCursor()的第一个参数传递给
$ b $返回index.openCursor(singleKeyRange);


函数updateClienteDB(clave,valor,newvalor){
console.log(updateClienteDB ... clave:+ clave +valor:+ valor +newvalor: + newvalor);

//添加了这行。我们在这里创建事务,一旦
var transaction = db.transaction([customers],readwrite);

//这行改变了,我们只是在这里引用事务实例而不是
//创建第二个事务
var objectStore = transaction.objectStore(customers);

//这行改变了,我们将一个活动事务实例传递给函数
//,而不是让函数创建自己的事务
request = getObjectStoreClienteDB(交易,名称,勇气);

request.onsuccess = function(event){
//获取我们要更新的旧值
var data = request.result;

//更新对象中要更改的值
if(clave ==name)
data.name = newvalor;
else if(clave ==email)
data.email = newvalor;
else if(clave ==matricula)
data.matricula = newvalor;
else if(clave ==telefono)
data.telefono = newvalor;

//把这个更新的对象放回到数据库中。

//此行现在可用。 objectStore被附加到我们的1个事务中,这个事务仍然是'alive'
//因为它被'及时'注册(事务之前由于
超时而结束)//在允许的时间窗口中没有注册额外的请求)。
var requestUpdate = objectStore.put(data);

$ b requestUpdate.onerror = function(event){
console.log(addCliente ...+ name ++ email ++ event.target.errorCode );
};
requestUpdate.onsuccess = function(event){
console.log(All done!);
};
};
}

另外,这里是第二个例子。它使用调用者的事务重新创建对对象存储的引用,而不是交叉引用附加到其他事务的对象存储:

  function updateClienteDB(clave,valor,newvalor){
console.log(updateClienteDB ... clave:+ clave +valor:+ valor +newvalor:+ newvalor);
var objectStore = db.transaction([customers],readwrite)。objectStore(customers);

request = getObjectStoreClienteDB(name,valor);

request.onsuccess = function(event){
//获取我们要更新的旧值
var data = request.result;

//更新对象中要更改的值
if(clave ==name)
data.name = newvalor;
else if(clave ==email)
data.email = newvalor;
else if(clave ==matricula)
data.matricula = newvalor;
else if(clave ==telefono)
data.telefono = newvalor;

//把这个更新的对象放回到数据库中。

//下面的行被改变,以便它可以工作。
// var requestUpdate = objectStore.put(data);

var theOtherTransactionThatIsStillAlive = event.transaction;
var objectStoreFromValidTransaction = theOtherTransactionThatIsStillAlive.objectStore('customers');

var requestUpdate = objectStoreFromValidTransaction.put(data);

requestUpdate.onerror = function(event){
console.log(addCliente ...+ name ++ email ++ event.target.errorCode);
};
requestUpdate.onsuccess = function(event){
console.log(All done!);
};
};
}


Hi I am trying to update records indexdb. I want to implement a generic method that allows me to upgrade to any field.

To do this I have a function that returns me the record I need.

function getObjectStoreClienteDB(clave,valor){
    //Ahora recogeremos los datos de nuestro almacén "Productos"
    var transaction = db.transaction(["customers"], "readwrite");
    var objectStore = transaction.objectStore("customers");

    var index = objectStore.index(clave);
    var singleKeyRange = IDBKeyRange.only(valor);

    // To use one of the key ranges, pass it in as the first argument of openCursor()/openKeyCursor()

    return  index.openCursor(singleKeyRange);
} 

And another that updates the returned record

function updateClienteDB(clave,valor,newvalor){
    console.log("updateClienteDB ... clave: "+clave+" valor: "+valor+" newvalor: "+newvalor);
    var objectStore = db.transaction(["customers"], "readwrite").objectStore("customers");

    request = getObjectStoreClienteDB("name",valor);

    request.onsuccess = function(event) {
      // Get the old value that we want to update
      var data = request.result;

      // update the value(s) in the object that you want to change
      if(clave=="name")
        data.name = newvalor;
       else if(clave=="email")
        data.email = newvalor;
       else if(clave=="matricula")
        data.matricula = newvalor;
       else if(clave=="telefono")
        data.telefono = newvalor;

      // Put this updated object back into the database.
      var requestUpdate = objectStore.put(data);
       requestUpdate.onerror = function(event) {
          console.log("addCliente ..."+name+" "+email +" "+ event.target.errorCode);
       };
       requestUpdate.onsuccess = function(event) {
            console.log("All done!");
       };
    };
}

In the line: var requestUpdate = objectStore.put(data);

error: Uncaught TransactionInactiveError: Failed to execute 'put' on 'IDBObjectStore': The transaction has finished.

解决方案

Try using the same readwrite transaction. It looks like you are calling db.transaction(...) in two places. You are then trying to update an object store linked to a different transaction in the context of a different transaction. The result is that one of the transactions completes (its lifetime ends), probably because it times out, because it does not detect any registered requests. The objectStore variable lives within the lifetime of the transaction that completes early, so you get a transaction finished error.

This is trivial to fix. You simply need to register a new request (the put request) on the same transaction before its lifetime ends. To do this, you can either (a) inline the statement request = getObjectStoreClienteDB("name",valor); so that it is attached to the same transaction instance, or (b) store the transaction instance in a separate variable and reference that in both calls.

For example, the following code is a modification of yours that uses a single transaction:

// this function was changed, it now takes an active transaction as its first argument.

function getObjectStoreClienteDB(transaction, clave,valor){
  //Ahora recogeremos los datos de nuestro almacén "Productos"

  // this was changed. instead of creating a new transaction we just reference the
  // transaction instance passed to this function
  var objectStore = transaction.objectStore("customers");

  var index = objectStore.index(clave);
  var singleKeyRange = IDBKeyRange.only(valor);

  // To use one of the key ranges, pass it in as the first argument of openCursor()/openKeyCursor()

  return  index.openCursor(singleKeyRange);
} 

function updateClienteDB(clave,valor,newvalor){
  console.log("updateClienteDB ... clave: "+clave+" valor: "+valor+" newvalor: "+newvalor);

  // this line was added. we create the transaction here, once
  var transaction = db.transaction(["customers"], "readwrite");

  // this line was changed, we simply reference the transaction instance here instead of 
  // creating a second transaction
  var objectStore = transaction.objectStore("customers");

  // this line was  changed, we pass in the one active transaction instance to the function
  // now instead of having the function create its own transaction
  request = getObjectStoreClienteDB(transaction, "name",valor);

  request.onsuccess = function(event) {
    // Get the old value that we want to update
    var data = request.result;

    // update the value(s) in the object that you want to change
    if(clave=="name")
      data.name = newvalor;
    else if(clave=="email")
      data.email = newvalor;
    else if(clave=="matricula")
      data.matricula = newvalor;
    else if(clave=="telefono")
      data.telefono = newvalor;

    // Put this updated object back into the database.

    // this line now works. objectStore is attached to our 1 transaction that is still 'alive' 
    // because it gets registered 'in time' (before transaction finishes due to timeout due 
    // to no additional requests registered in allowed time window).
    var requestUpdate = objectStore.put(data);


    requestUpdate.onerror = function(event) {
      console.log("addCliente ..."+name+" "+email +" "+ event.target.errorCode);
    };
    requestUpdate.onsuccess = function(event) {
      console.log("All done!");
    };
  };
}

And, just to be clear, here is a second example. It re-creates a reference to the object store using the transaction of the caller, instead of cross referencing an object store attached to a different transaction:

function updateClienteDB(clave,valor,newvalor){
  console.log("updateClienteDB ... clave: "+clave+" valor: "+valor+" newvalor: "+newvalor);
  var objectStore = db.transaction(["customers"], "readwrite").objectStore("customers");

  request = getObjectStoreClienteDB("name",valor);

  request.onsuccess = function(event) {
    // Get the old value that we want to update
    var data = request.result;

    // update the value(s) in the object that you want to change
    if(clave=="name")
      data.name = newvalor;
    else if(clave=="email")
      data.email = newvalor;
    else if(clave=="matricula")
      data.matricula = newvalor;
    else if(clave=="telefono")
      data.telefono = newvalor;

    // Put this updated object back into the database.

    // the following line is changed so that it works. 
    //var requestUpdate = objectStore.put(data);

    var theOtherTransactionThatIsStillAlive = event.transaction;
    var objectStoreFromValidTransaction = theOtherTransactionThatIsStillAlive.objectStore('customers');

    var requestUpdate = objectStoreFromValidTransaction.put(data);

    requestUpdate.onerror = function(event) {
      console.log("addCliente ..."+name+" "+email +" "+ event.target.errorCode);
    };
    requestUpdate.onsuccess = function(event) {
      console.log("All done!");
    };
  };
}

这篇关于更新indexedDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆