Unable to get property of undefined or null reference UWP

0

I have the following code in my visual studio and it works perfectly well au a UWP app on my desktop win10, albeit it does not work on my windows phone as a UWP app. I also tried running my simple webapp as from a webserver and loading it in the Edge and it works perfectly. What should be the problem? My code looks like this. I omitted some parts:

var model = {
    db: {},
    goalsobj: {},
    goals: [],
    init: function() {
        var openReq = window.indexedDB.open("GoalsDB");
        openReq.onupgradeneeded = function (event) {
            model.db = event.target.result;
            var objectStore = model.db.createObjectStore("Goals", { keyPath: "id" });
            objectStore.createIndex("id","id", {unique:true});
        };
        openReq.onsuccess = function (event) {
            model.db = event.target.result
            model.db.transaction("Goals", "readonly").objectStore("Goals").count().onsuccess = function (event) {
                if (event.target.result == 0) {
                    console.log("indexeddb empty");
                    var goalstemplate = {
                        id: "idee",
                        goals: [{ "name": "Task1" }, { "name": "Task2" }, { "name": "Task3" }]
                        }
                    var addReq = model.db.transaction("Goals", "readwrite").objectStore("Goals").add(goalstemplate);
                } else {
                    model.db.transaction("Goals", "readonly").objectStore("Goals").get("idee").onsuccess = function (e) {
                        model.goalsobj = e.target.result;
                        //console.log(e.target.result);
                        model.goals = model.goalsobj.goals;
                        goalfunc.makeList(); //model should not talk to view, but this case it is amust, because if I remove this, it does not render at boot.
                        }

                }

            }
    openReq.onerror = function (event) {
        console.log("Operation failed");
    }

}
    },
    add: function(goalname) {
        model.goals.push(
            {
                "name": goalname
        });
        model.savedb();
    },
    move: function (id,updown) {
        if (updown == "up") {
            model.goals.splice((id-1), 0, model.goals.splice(id, 1)[0]);
        };
        if (updown == "down") {
            model.goals.splice((id+1), 0, model.goals.splice(id, 1)[0]);
        };
    },
    savedb: function(){ 
        //console.log(goals);
        var update = model.db.transaction("Goals", "readwrite").objectStore("Goals").put(model.goalsobj);
        update.onerror = function (event) {
            console.log(event);
        };

},
};

Now When I rund this cond on my device it sais: Unhandled exception at line 28, column 25 in ms-appx-web://1318f74a-397e-4958-aa6b-c8d11b7c5dce/js/main.js

0x800a138f - JavaScript runtime error: Unable to get property 'goals' of undefined or null reference

javascript
windows-phone
uwp
asked on Stack Overflow Jan 4, 2017 by Peter

1 Answer

0

I have tested your code in my device (Device: Microsoft RM-1118 OSVersion:WindowsMobile 14393). It is working fine. As you can see I placed a button on the html page. The action of button click will execute model.init(), and then I set a break-point at model.goals = model.goalsobj.goals;. When click button the second time and model.goals will be set right value.

So I think the issue may happen in your target device or your GoalsDB was destroyed. Because the cause of Unable to get property 'goals' of undefined or null reference is that model.goalsobj was not set right value. Please check whether those operations have changed your database structure, such as moving operation. You can show more detail about your target device, and I will help you.

(function () {
    document.getElementById("createDatabase").addEventListener("click", createDB, false);
    function createDB() {   
        model.init();
    }
})();
var model = {
    db: {},
    goalsobj: {},
    goals: [],
    init: function () {
        var openReq = window.indexedDB.open("GoalsDB");
        openReq.onupgradeneeded = function (event) {
            model.db = event.target.result;
            var objectStore = model.db.createObjectStore("Goals", { keyPath: "id" });
            objectStore.createIndex("id", "id", { unique: true });
        };
        openReq.onsuccess = function (event) {
            model.db = event.target.result
            model.db.transaction("Goals", "readonly").objectStore("Goals").count().onsuccess = function (event) {
                if (event.target.result == 0) {
                    console.log("indexeddb empty");
                    var goalstemplate = {
                        id: "idee",
                        goals: [{ "name": "Task1" }, { "name": "Task2" }, { "name": "Task3" }]
                    }

                    model.db.transaction("Goals", "readwrite").objectStore("Goals").add(goalstemplate);

                } else {
                    model.db.transaction("Goals", "readonly").objectStore("Goals").get("idee").onsuccess = function (e) {

                        model.goalsobj = e.target.result;
                        //console.log(e.target.result);
                        if (model.goalsobj.goals != undefined) {
                            model.goals = model.goalsobj.goals;
                        } else {
                            console.log(e.target.result);
                        }

                       //goalfunc.makeList(); //model should not talk to view, but this case it is amust, because if I remove this, it does not render at 
                    }
                }
            }
            openReq.onerror = function (event) {
                console.log("Operation failed");
            }
        }
    },
    add: function (goalname) {
        model.goals.push(
            {
                "name": goalname
            });
        model.savedb();
    },
    move: function (id, updown) {
        if (updown == "up") {
            model.goals.splice((id - 1), 0, model.goals.splice(id, 1)[0]);
        };
        if (updown == "down") {
            model.goals.splice((id + 1), 0, model.goals.splice(id, 1)[0]);
        };
    },
    savedb: function () {
        //console.log(goals);
        var update = model.db.transaction("Goals", "readwrite").objectStore("Goals").put(model.goalsobj);
        update.onerror = function (event) {
            console.log(event);
        };
    }
};
answered on Stack Overflow Jan 5, 2017 by Nico Zhu - MSFT • edited Jan 6, 2017 by Nico Zhu - MSFT

User contributions licensed under CC BY-SA 3.0