this.collection.reset引发错误-未捕获类型错误:无法读取属性'原型'的未定义

this.collection.reset throwing error - Uncaught TypeError: Cannot read property 'prototype' of undefined

本文关键字:错误 读取 属性 未定义 原型 类型 reset collection this      更新时间:2023-09-26

我试图重置集合,但当我这样做时,我会得到以下错误,

未捕获的类型错误:无法读取未定义的属性"prototype">

触发此重置的代码如下,

// Main object for the entire app

窗口。APP={

config: {
    api: {
        base:'http://app.local/api/v1/',
    }
},
// Create this closure to contain the cached modules
module: function() {
    // Internal module cache.
    var modules = {};
    // Create a new module reference scaffold or load an
    // existing module.
    return function(name) {
        // If this module has already been created, return it.
        if(modules[name]) {
            return modules[name];
        }
        // Create a module and save it under this name
        modules[name] = { Views: {} };
        return modules[name];
    };
}(),
init: function() {
    // :: app start :: //
    var app = POPS;
    var Module = app.module( $( '#popsapp' ).data('route') );
    // Creates a Master object in the global namespace so data can be passed in from the DOM.
    // This would be replaced with a master Router if we weren't using actual pages
    app.Initialiser = function( initialCollection ) {
        this.start = function() {
            //don't cache ajax calls
            $.ajaxSetup({ cache: false });
            console.log(Module);
            if(Module.Collection !== undefined) {
                this.collection = new Module.Collection();
                this.view = new Module.Views.Master({ collection: this.collection });
            } else {
                this.view = new Module.Views.Master();  
            }
            if(this.collection !== undefined) {
                console.log(this.collection);
                console.log(initialCollection);
                console.log(this.collection.reset( initialCollection ));
                this.collection.reset( initialCollection );
            }
            //moved this here so script runs after the DOM has loaded.
            //but script.js still needs completely removing.
        };
    }; 
}

};

//应用程序的入口点APP.int((;

如果我在此之前记录this.collection,则会在this.collection.reset( initialCollection)处触发错误。我试图从中创建集合的JSON如下,

[
{
    "name": "Organisation 1",
    "information": "This is some information about the organisation!",
    "notifications": "0",
    "add_all": "0",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "organisation",
    "clients": [
        {
            "id": "1",
            "name": "Client 1",
            "information": "A client called Client 1",
            "add_all": "0",
            "created_at": "-0001-11-30 00:00:00",
            "updated_at": "-0001-11-30 00:00:00",
            "type": "client",
            "pivot": {
                "organisation_id": "1",
                "client_id": "1"
            }
        },
        {
            "id": "2",
            "name": "Client 2",
            "information": "A client called Client 2",
            "add_all": "1",
            "created_at": "-0001-11-30 00:00:00",
            "updated_at": "-0001-11-30 00:00:00",
            "type": "client",
            "pivot": {
                "organisation_id": "1",
                "client_id": "2"
            }
        }
    ],
    "projects": [
        {
            "id": "1",
            "name": "Project #1",
            "description": "Descriptive text.",
            "total_cost": "0.00",
            "start_date": "2014-01-01",
            "finish_date": "2014-12-31",
            "run_number_days": "365",
            "num_days_from_year_start": "0",
            "color": "#92e807",
            "created_at": "-0001-11-30 00:00:00",
            "updated_at": "-0001-11-30 00:00:00",
            "user_id": "1",
            "pivot": {
                "organisation_id": "1",
                "project_id": "1"
            }
        }
    ],
    "members": [],
    "teams": []
},
{
    "name": "Organisation 2",
    "information": "This is some information about Organisation 2",
    "notifications": "0",
    "add_all": "0",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "organisation",
    "clients": [],
    "projects": [],
    "members": [],
    "teams": []
},
{
    "name": "Client 1",
    "information": "A client called Client 1",
    "add_all": "0",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "client",
    "members": [
        {
            "id": "1",
            "email": "user@domain.com",
            "first_name": "User",
            "last_name": "Name",
            "display_name": "",
            "initials": null,
            "remember_me": null,
            "login_type": "normal",
            "api_token": null,
            "created_at": "-0001-11-30 00:00:00",
            "updated_at": "-0001-11-30 00:00:00",
            "deleted_at": null,
            "pivot": {
                "client_id": "1",
                "user_id": "1"
            }
        }
    ]
},
{
    "name": "Client 2",
    "information": "A client called Client 2",
    "add_all": "1",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "client",
    "members": []
}

]

json是完全有效的,我不确定问题出在哪里。有人有什么想法吗,它读起来像是this.collection没有定义,这就是为什么我不能在它上运行reset((,但当我记录它时,我可以看到collciton。

collection.reset()方法将骨干模型的数组作为输入。您的数组(initialCollection(只是一个通用的JavaScript数组。