未被捕获的TypeError:无法读取空Knockout js的属性“ map”

时间:2019-01-04 21:39:59

标签: javascript knockout.js

获取未捕获的TypeError:无法在我的js基因剔除文件中读取null的属性“ map”。我以前没有遇到过这个问题,对我来说很好。我在这里想念东西吗?

代码:

var ProcessorReviewerAssignmentViewModel = function (obj, token) {
    obj = obj || {
        PrsnPk: 0,
        MaxCapacity: 0,
        RemainCapacity: 0,
        MinLoanAmt: 0,
        MaxLoanAmt: 0,
        MinAppraisalScore: 0,
        MaxAppraisalScore: 0,
        MinBpoScore: 0,
        MaxBpoScore: 0,
        AutoAssignEligible: false,
        IsGetNext: false,
        IsHighValueReady: false,
        IsAppraiserScore: false,
        ReviewTiers: [],
        LoanClassifications: [],
        AppraisalScoreRanges: [],
        BPOAppraisalScoreRanges: [],
        CurrentAppraiserScoreSelection: 0,
        CurrentBPOAppraiserScoreSelection: 0,

        //For Reviewer Services
        ReviewerServices: []
    }

    var self = this;

    //Value to determine if any of the values in Services are selected or not.
    //So if any services aren't selected, we want to select ALL services, otherwise deselect ALL services.
    var isUnChecked = ko.observableArray();

    //set up anti-forgery tokens for ajax calls to server
    $.ajaxSetup({
        headers: { 'RequestVerificationToken': token }
    });

    //observables to mimic the server model
    self.PrsnPk = ko.observable(obj.PrsnPk);
    self.MaxCapacity = ko.observable(obj.MaxCapacity);
    self.RemainCapacity = ko.observable(obj.RemainCapacity);
    self.MinLoanAmt = ko.observable(obj.MinLoanAmt);
    self.MaxLoanAmt = ko.observable(obj.MaxLoanAmt);
    self.MinAppraisalScore = ko.observable(obj.MinAppraisalScore);
    self.MaxAppraisalScore = ko.observable(obj.MaxAppraisalScore);
    self.MinBpoScore = ko.observable(obj.MinBpoScore);
    self.MaxBpoScore = ko.observable(obj.MaxBpoScore);
    self.IsAppraiserScore = ko.observable(obj.IsAppraiserScore);
    self.AutoAssignEligible = ko.observable(obj.AutoAssignEligible);
    self.IsGetNext = ko.observable(obj.IsGetNext);
    self.IsHighValueReady = ko.observable(obj.IsHighValueReady);
    self.CurrentAppraiserScoreSelection = ko.observable(obj.CurrentAppraiserScoreSelection);
    self.CurrentBPOAppraiserScoreSelection = ko.observable(obj.CurrentBPOAppraiserScoreSelection);
    //AppInstId for Reviewer Services
    self.AppInstId = ko.observable(obj.AppInstId);

    //map arrays to mimic the server objects created from db
    self.ReviewTiers = ko.observableArray(obj.ReviewTiers.map(function (element) {
        return {
            IsSelected: ko.observable(element.IsSelected),
            Description: ko.observable(element.Description),
            ReviewTierId: ko.observable(element.ReviewTierId)
        }
    }));

    self.LoanClassifications = ko.observableArray(obj.LoanClassifications.map(function (element) {
        return {
            IsSelected: ko.observable(element.IsSelected),
            SystemCode: ko.observable(element.SystemCode),
            Description: ko.observable(element.Description)
        }
    }));

    self.AppraisalScoreRanges = ko.observableArray(obj.AppraisalScoreRanges.map(function (element) {
        return {
            IsSelected: ko.observable(element.IsSelected),
            Description: ko.observable(element.Description),
            AppraisalScoreRangeID: ko.observable(element.AppraisalScoreRangeID)
        }
    }));

    self.BPOAppraisalScoreRanges = ko.observableArray(obj.BPOAppraisalScoreRanges.map(function (element) {
        return {
            IsSelected: ko.observable(element.IsSelected),
            Description: ko.observable(element.Description),
            AppraisalScoreRangeID: ko.observable(element.AppraisalScoreRangeID)
        }  

    }));

    //Observable array to keep track of Reviewer services selected in view
    **self.ReviewerServices = **ko.observableArray(obj.ReviewerServices.map(function (element)**** {
        if (element.IsSelected === false) {
            isUnChecked.push(element.IsSelected);
        }
        return {
            ServiceId: ko.observable(element.ServiceId),
            ServiceAliasDescription: ko.observable(element.ServiceAliasDescription),
            IsSelected: ko.observable(element.IsSelected)
        }
    }));

错误发生在:

 self.ReviewerServices = ko.observableArray(obj.ReviewerServices.map(function (element)

我认为这可能是因为加载此页面时ReviewerServices可能为null?

感谢您的输入。

1 个答案:

答案 0 :(得分:2)

在这里进行猜测,但是由于您的代码以obj = obj || { ... }开始,因此似乎用不包含ReviewerServices的对象调用了函数。简单的解决方法是添加

obj.ReviewerServices = obj.ReviewerServices || [];

obj = obj || { ... }后面的行中,或者如果要确保所有这些属性都设置为默认值(如果它们在obj中不存在,则:

obj =  {
  ...{
      PrsnPk: 0,
      MaxCapacity: 0,
      RemainCapacity: 0,
      MinLoanAmt: 0,
      MaxLoanAmt: 0,
      MinAppraisalScore: 0,
      MaxAppraisalScore: 0,
      MinBpoScore: 0,
      MaxBpoScore: 0,
      AutoAssignEligible: false,
      IsGetNext: false,
      IsHighValueReady: false,
      IsAppraiserScore: false,
      ReviewTiers: [],
      LoanClassifications: [],
      AppraisalScoreRanges: [],
      BPOAppraisalScoreRanges: [],
      CurrentAppraiserScoreSelection: 0,
      CurrentBPOAppraiserScoreSelection: 0,
      ReviewerServices: []
  }, 
  ...obj
}

代替obj = obj || { ... }