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

Unable to get property of undefined or null reference UWP

我的 visual studio 中有以下代码,它在我的桌面 win10 上的 UWP 应用程序中运行良好,尽管它不适用于我的 windows phone 作为UWP 应用程序。我还尝试了 运行 我的简单 web 应用程序,来自网络服务器并将其加载到 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);
        };

},
};

现在,当我在我的设备上运行这个条件时,它说: ms-appx-web://1318f74a-397e-4958-aa6b-c8d11b7c5dce/js/main.js

中第 28 行第 25 列的未处理异常

0x800a138f - JavaScript 运行时错误:无法获取 属性 'goals' 未定义或空引用

我已经在我的设备(设备:Microsoft RM-1118 OSVersion:WindowsMobile 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);
        };
    }
};