无法获取未定义或空引用的属性UWP

时间:2017-01-04 15:14:55

标签: javascript windows-phone uwp

我的视觉工作室中有以下代码,它在我的桌面win10上的UWP应用程序中运行得非常好,尽管它在我的Windows手机上不能用作UWP应用程序。我也尝试从网络服务器运行我的简单webapp并将其加载到Edge中,它完美地运行。 应该是什么问题? 我的代码看起来像这样。我省略了一些部分:

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);
        };

},
};

现在,当我在我的设备上使用这个cond时,它是sais: ms-appx-web://1318f74a-397e-4958-aa6b-c8d11b7c5dce/js/main.js第28行第28行未处理的异常

0x800a138f - JavaScript运行时错误:无法获取未定义或空引用的属性“目标”

1 个答案:

答案 0 :(得分:0)

我已在设备中测试了您的代码(设备:Microsoft RM-1118 OSVersion:WindowsMo​​bile 14393)。它工作正常。如你所见,我在html页面上放了一个按钮。按钮单击操作将执行model.init(),然后我在model.goals = model.goalsobj.goals;设置断点。第二次单击按钮时,model.goals将设置正确的值。

所以我认为问题可能发生在你的目标设备上,或者你的GoalsDB被破坏了。因为Unable to get property 'goals' of undefined or null reference的原因是model.goalsobj没有设置正确的值。请检查这些操作是否已更改数据库结构,例如移动操作。您可以显示有关目标设备的更多详细信息,我会帮助您。

(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);
        };
    }
};