Languages

Menu
Sites
Language
Indexed DB has a problem.

Hi, I I have a error about IndexedDB (specially transaction).

 
In following code, the error,  "TypeError: 'undefined' is not an object (evaluating 'db.transaction')", is occured.
 
var request2 = db.transaction(["users"], "readwrite").objectStore("users")
 
I try to solve this problem. but I can't..
please help me to use Indexed DB in Tizen.
 
 
Here is my code:
(I can't write email address in the code, so I change it something like 'bill_email')
-------------------------------------------------------------------------------------------------------------
const peopleData = [
  { name: "Bill", age: 35, email: "bill_email" },
  { name: "Donna", age: 32, email: "donna_email" }
];
 
$(window).load(function(){
document.addEventListener('tizenhwkey', function(e) {
        if(e.keyName == "back")
try {
    tizen.application.getCurrentApplication().exit();
} catch (ignore) {
}
    });
console.log("sss");
 
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
 
//prefixes of window.IDB objects
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange
 
if (!indexedDB) {
    alert("Your browser doesn't support a stable version of IndexedDB.")
 
var userData = [
                { id: "1", name: "Tapas", age: 33, email: "Tapas_email" },
                { id: "2", name: "Bidulata", age: 55, email: "bidu_email" }
              ];
 
var db;
var request = indexedDB.open("databaseName", 1);
 
request.-nerror = function(e) {
  console.log("error: ", e);
};
 
request.-nsuccess = function(e) {
  db = request.result;
  console.log("success: "+ db);
};
 
request.-nupgradeneeded = function(event) {
        var objectStore = event.target.result.createObjectStore("users", {keyPath: "id"});
        for (var i in userData) {
                objectStore.add(userData[i]);    
                console.log("222");
        }
}
 
var request2 = db.transaction(["users"], "readwrite").objectStore("users")
     .add({ id: "3", name: "Gautam", age: 30, email: "Gautam_email" });
                     
request2.-nsuccess = function(e) {
console.log("Gautam has been added to the database.");
};
 
request2.-nerror = function(e) {
console.log("Unable to add the information.");       
}
 
});

Responses

1 Replies
AVSukhov

Hello,

You try perfrom db.transaction before db object is initiliazed in request.onsuccess

This is what causes your error (db is undefined).

Before calling db transaction be sure that db object is initiliazed and object store is created.

For examole you can use transition.oncomplete callback.

For more information please refer to tutorial:

https://developer.tizen.org/dev-guide/2.3.0/org.tizen.mobile.web.appprogramming/html/tutorials/w3c_tutorial/storage_tutorial/indexed_db_tutorial.htm