无法读取属性' MessageList'未定义的

时间:2018-04-24 09:30:03

标签: javascript jquery knockout.js

我想,MessageList推送这个值,但是我收到了错误。

错误是" TypeError:无法读取属性' MessageList'未定义"

当我删除item.Message List.removeAll();我接受了这个错误"结果未定义" 这是我的bindList,我把这个块搞错了

   function bindList(data) {

    try {
    var results2 = ko.observableArray();
    ko.mapping.fromJS(data.list, {}, results2);
    item.MessageList.removeAll();

    for (var i = 0; i < results2().length; i++) {

        item.MessageList.push(
            new forum(
                results()[i].threadID(),
                results()[i].sender(),
                results()[i].files(),
                results()[i].messagesStatus(),
                results()[i].createdDate(),
                results()[i].forumMessageID(),
                results()[i].message(),
                //results()[i].whoReplyForumMessageID(),
                results()[i].CreatedByUserId,
                results()[i].senderID,
                results()[i].thread

            )
        );
        }
    }

    catch(err) {
        console.log("Hata"+err);

    }
}

这是我的LoadFromServer

 function LoadFromServer(id, eid) {

    blmsCommon.showLoading();

    var json = {};
    json.id = id;
    json.eid = eid;
    console.log(json);
    $.ajax({
        type: "GET",
        url:'/Forum/GetMessages',
        dataType: 'JSON',
        data: { id: JSON.stringify(json) },
        contentType: false,
        cache: false,
        success: function (data) {
            //var totalC = data.totalCount;
           // var itemCountInPage = data.countInPage;

                bindList(data);
                blmsCommon.hideLoading();
                console.log(data);
            console.log("LoadFromServer");


        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }       
    });
}

我用淘汰创建了视图模型,我创建了&#34; var item;&#34;

 var viewModel = function () {       
    var self = this;
    this.threadId = ko.observable();
    this.sender = ko.observable();
    this.files = ko.observable();
    this.messagesStatus = ko.observable();
    this.CreatedDate = ko.observable();
    this.forumMessageID = ko.observable();
    this.message = ko.observable();
   // this.whoReplyForumMessageID = ko.observable();
    this.CreatedByUserId = ko.observable();
    this.senderID = ko.observable();
    this.thread = ko.observable();
    this.MessageList = ko.observableArray();

    /*paging*/
    this.currentPageNumber = ko.observable();
    this.totalPageNumber = ko.observable();
    this.totalCount = ko.observable();
    this.firstIndex = ko.observable();
    this.lastIndex = ko.observable();
    this.order = ko.observable();
    /*paging*/
}

var item;

2 个答案:

答案 0 :(得分:0)

也许你没有在开始时初始化你的MessageList。尝试做:

var MessageList = [];

第一次声明MessageList时。

答案 1 :(得分:0)

您需要使用new viewModel()代替viewModel()。 因为New Operator创建viewModel类型的实例。 这有助于您了解有关this关键字的更多信息。