Undefined object reading from Indexeddb (JS)

Asked

Viewed 24 times

0

I’m using Indexeddb to record a simple table using Javascript. When selecting a record for change, it is read and loaded into the form. However, when trying to update it in the database it is not read and returns an undefined object. It follows the Update function I did:

function updtCliente(iID){ // <-- O código vem corretamente para a função
   var oStr = oDB.transaction(['clientes'], 'readwrite')
                 .objectStore('clientes');
   var rReq = oStr.get(iID);

   rReq.onerror = function(e){
      Mensagem('Não foi possível ler o registro.');
   };

   rReq.onsuccess = function(e){
      var dData = rReq.result; // <-- A variável "dData" fica "undefined"
      dData.cli_nome = $("#cli_nome").val();
      dData.cli_email = $("#cli_email").val();
      dData.cli_telefone = $("#cli_telefone").val();
      dData.cli_ativo = ($("input[name=cli_ativo]:checked").val() == 'ativo') ? '1' : '0';

      var rUpdt = oStr.put(dData);

      rUpdt.onerror = function(e){
         Mensagem('Erro na atualização.');
      };

      rUpdt.onsuccess = function(e){
         window.location.reload(false);
      };
   };
}

1 answer

0


Change:

var dData = rReq.result; // <-- A variável "dData" fica "undefined" 

for

var dData = e.target.result;
  • Jorge, I’ve already done this... I’ve already concluded the reading of the record with the success function yet it wasn’t: "oStr.get(iID). onsuccess = Function(e){var dData = e.target.result; ...."

  • Are you sure that key is in the database

  • It is. When loading the form I want to edit, it brings the data filled with the ID together. When using debug, I see that the data is in the database. Even passing with debug, when giving "onsuccess" the variable "e.target.result" is "Undefined". I use this same command to load the data in the form to change... It’s very strange this...

  • Put the code you use to load the client data and do console.log(iID) to make sure you’re getting the right key

  • Jorge, I thought! Going through the ". get(iID)" this variable was as string. I was able to solve this by putting ". get(parseint(iID))". The ID in the database is auto-incremental and I didn’t notice that it was passing string, since when loading the form the variable came as integer. Thank you so much for your help. I will mark as solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.