locked
why evt.target.result is null in indexDB? RRS feed

  • Question

  • var indexedDB = window.msIndexedDB;
    
    adsageIDB = {};
    adsageIDB.db = null;
    
    adsageIDB.onerror = function (e) {
        console.log(e);
    };
    
    adsageIDB.open = function () {
        var request = indexedDB.open("adsageIDB", 1);
    
        request.onsuccess = function (evt) {
            adsageIDB.db = evt.target.result;
            var db = adsageIDB.db;
            if (db.objectStoreNames.contains("todo")) {
                db.deleteObjectStore("todo");
            }
            var store = db.createObjectStore("todo",{ keyPath: "adsid" });
    
        };
        request.onerror = function (evt) {
            console.log("Database error code:" + evt.target.errorCode);
        };
    }
    why evt.target.result is null in indexDB? what's my error?
    Tuesday, April 10, 2012 9:40 AM

Answers

  • Hi,

    You need to handle the db after the onsuccess is called. Namely you can use it after the bold lines below.

    app.onactivated = function (eventObject) {
            if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
                if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize 
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension. 
                    // Restore application state here.
                }
                WinJS.UI.processAll();
    
    
                var indexedDB = window.msIndexedDB;
    
                var request = indexedDB.open("testDB", 1);
                request.onsuccess = function (evt) {
                    var test = evt.target.result;
                    console.log(test);//It's not null and you can call other code after this
                }
            }
        };

    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Thursday, April 12, 2012 9:26 AM

All replies

  • Hi,

    I'm unable to reproduce this problem. Are you using public Consumer Preview version? Does it work if you create a blank new Metro Style application? If it still doesn't work please post all the code you added in the blank new application.


    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.




    Wednesday, April 11, 2012 9:40 AM
  • i create a new Grid Application , and add a indexDBHelper.js in js folder,code is:

    var indexedDB = window.msIndexedDB;
    
    adsageIDB = {};
    adsageIDB.db = null;
    
    adsageIDB.onerror = function (e) {
        console.log(e);
    };
    
    adsageIDB.open = function () {
        var request = indexedDB.open("adsageIDB", 1);
    
        request.onsuccess = function (evt) {
            adsageIDB.db = evt.target.result;
            var db = adsageIDB.db;
            if (db.objectStoreNames.contains("todo")) {
                db.deleteObjectStore("todo");
            }
            var store = db.createObjectStore("todo",{ keyPath: "adsid" });
    
        };
        request.onerror = function (evt) {
            console.log("Database error code:" + evt.target.errorCode);
        };
    }
    
    adsageIDB.addTodo = function (todoText) {
        var db = adsageIDB.db;
        var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);
        var store = trans.objectStore("todo");
        var data = {
            "text": todoText,
            "adsid": new Date().getTime()
        };
        var request = store.put(data);
        request.onsuccess = function (e) {
            console.log("Success Add: ", e);
        };
        request.onerror = function (e) {
            console.log("Error Adding: ", e);
        };
    };
    
    adsageIDB.deleteTodo = function (id) {
        var db = adsageIDB.db;
        var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);
        var store = trans.objectStore("todo");
        var request = store.delete (id);
        request.onsuccess = function (e) {
            console.log("Success Delete: ", e);
        };
        request.onerror = function (e) {
            console.log("Error Adding: ", e);
        };
    };
    
    adsageIDB.getTodo = function (id) {
        var db = adsageIDB.db;
        var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);
        var store = trans.objectStore("todo");
        var item = store.get(id);
        return item;
    };

    then add:

    <script src="/js/indexDBHelper.js"></script>

    to default.html (before <script src="/js/data.js"></script>),then press F5 , i get the error:

    Wednesday, April 11, 2012 10:08 AM
  • I could not duplicate this in my system.

    "Strictly" speaking, I believe the line:

    adsageIDB = {};

    should probably be:

    var adsageIDB = {}; 

    but that does not seem to be the cause of your problem.

    Wednesday, April 11, 2012 1:42 PM
  • i create a new Grid Application , and add a indexDBHelper.js in js folder,code is:

    var indexedDB = window.msIndexedDB;
    
    adsageIDB = {};
    adsageIDB.db = null;
    
    adsageIDB.onerror = function (e) {
        console.log(e);
    };
    
    adsageIDB.open = function () {
        var request = indexedDB.open("adsageIDB", 1);
    
        request.onsuccess = function (evt) {
            adsageIDB.db = evt.target.result;
            var db = adsageIDB.db;
            if (db.objectStoreNames.contains("todo")) {
                db.deleteObjectStore("todo");
            }
            var store = db.createObjectStore("todo",{ keyPath: "adsid" });
    
        };
        request.onerror = function (evt) {
            console.log("Database error code:" + evt.target.errorCode);
        };
    }
    
    adsageIDB.addTodo = function (todoText) {
        var db = adsageIDB.db;
        var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);
        var store = trans.objectStore("todo");
        var data = {
            "text": todoText,
            "adsid": new Date().getTime()
        };
        var request = store.put(data);
        request.onsuccess = function (e) {
            console.log("Success Add: ", e);
        };
        request.onerror = function (e) {
            console.log("Error Adding: ", e);
        };
    };
    
    adsageIDB.deleteTodo = function (id) {
        var db = adsageIDB.db;
        var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);
        var store = trans.objectStore("todo");
        var request = store.delete (id);
        request.onsuccess = function (e) {
            console.log("Success Delete: ", e);
        };
        request.onerror = function (e) {
            console.log("Error Adding: ", e);
        };
    };
    
    adsageIDB.getTodo = function (id) {
        var db = adsageIDB.db;
        var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);
        var store = trans.objectStore("todo");
        var item = store.get(id);
        return item;
    };

    then add:

    <script src="/js/indexDBHelper.js"></script>

    to default.html (before <script src="/js/data.js"></script>),then press F5 , i get the error:

    It seems is incomplete code. Where do you call the function you defined so that the code is executed? Could you post all the code you added to a blank new application that can reproduce this issue? And the error message seems is not related to your original question. I would test use below code. The bold lines are added to a blank new Metro Style application:

    app.onactivated = function (eventObject) {
            if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
                if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize 
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension. 
                    // Restore application state here.
                }
                WinJS.UI.processAll();
    
    
                var indexedDB = window.msIndexedDB;
    
                var request = indexedDB.open("testDB", 1);
                request.onsuccess = function (evt) {
                    var test = evt.target.result;
                    console.log(test);//It's not null
                }
            }
        };


    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.




    Thursday, April 12, 2012 5:41 AM
  • call code(data.js):

        sampleItems.forEach(function (item) {
            adsageIDB.open();
            adsageIDB.addTodo(item);
            list.push(item);
        });

    Thursday, April 12, 2012 5:53 AM
  • This is normal as the onsuccess is not executed at that time. The indexedDB.open is async function. You can add some code logic to wait for the onsuccess called or call .addTodo in onsuccess.

    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Thursday, April 12, 2012 7:34 AM
  • thanks your answer,but how can i do,can give some suggest?
    Thursday, April 12, 2012 7:37 AM
  • Hi,

    You need to handle the db after the onsuccess is called. Namely you can use it after the bold lines below.

    app.onactivated = function (eventObject) {
            if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
                if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize 
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension. 
                    // Restore application state here.
                }
                WinJS.UI.processAll();
    
    
                var indexedDB = window.msIndexedDB;
    
                var request = indexedDB.open("testDB", 1);
                request.onsuccess = function (evt) {
                    var test = evt.target.result;
                    console.log(test);//It's not null and you can call other code after this
                }
            }
        };

    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Thursday, April 12, 2012 9:26 AM