未捕获的类型错误:无法读取未定义的属性“basePrice”

Uncaught TypeError: Cannot read property 'basePrice' of undefined

本文关键字:未定义 读取 属性 basePrice 类型 错误      更新时间:2024-01-16

我不确定错误(未捕获的类型错误:无法读取未定义的属性'basePrice'(是什么意思,因为该值在第一部分中有明确定义。

当我展开错误消息时,它会标记更新的价格,然后是两个匿名函数。我不确定我是否应该寻找错别字或我是否使用了不正确的逻辑。

 <script>
(function() {
  var plans = {
    basic: {
      pricingChart: false,
      pricingChart2: false,
      basePrice: 0
    },
    standard: {
      pricingChart: false,
      pricingChart2: false,
      basePrice: 2.99
    },
    premium: {
      pricingChart: true,
      pricingChart2: false,
      basePrice: 49.99
    },
    enterprise: {
      pricingChart: true,
      pricingChart2: true,
      basePrice: 500
    }
  };
  var viewerLevels = {
    500: 10.99,
    2500: 10.99,
    5000: 29.99,
    10000: 39.99
  };
  var userLevels = {
    1: 10.99,
    2: 20.99,
    3: 30.99,
    4: 40.99
  };

    var selectedPlan = "free";
    var viewerLevel = null;
    var userLevel = null;
      $( ".plan .select-button" ).on( "click", function( event ) {
        selectedPlan = $( this ).closest( ".plan" ).attr( "data-plan" );
        var planInfo = plans[ selectedPlan ];
        if ( planInfo.pricingChart ) {
          $( "#pricingChart" ).removeClass( "not" );
        } else {
          $( "#pricingChart" ).addClass( "not" );
          viewerLevel = null;
        }
        if ( planInfo.pricingChart2 ) {
          $( "#pricingChart2" ).removeClass( "not" );
        } else {
          $( "#pricingChart2" ).addClass( "not" );
        userLevel = null;
        }
        var showPricingChart = "#topPricingChart";
        if ( planInfo.pricingChart ) {
          showPricingChart = "#pricingChart";
        }
        $( "html,body" ).animate({
          scrollTop: $( showPricingChart ).offset().top
        }, "slow" );
        updatePrice();
      });
      $( ".pricingLevel" ).on( "click", function() {
        viewerLevel = $( this ).attr( "data-viewer" );
        updatePrice();  
      });
      $( ".pricingLevel2" ).on( "click", function() {
        userLevel = $( this ).attr( "data-user" );
        updatePrice();
      });
          function updatePrice() {
            var basePrice = plans[ selectedPlan ].basePrice;
            var viewerPrice = 0;
            if ( viewerLevel ) {
              viewerPrice = viewerLevels[ viewerLevel ];
            }
            var userPrice = 0;
            if ( userLevel ) {
              userPrice = userLevels[ userLevel ];
            }
            var totalPrice = basePrice + viewerPrice + userPrice;
          $( ".pricingLargeAmount" ).text( totalPrice );
          }
        updatePrice();
})();
</script>

该错误表示未定义plans[ selectedPlan ]

selectedPlan == "free"plans.free不存在。

该错误意味着 selectedPlan 的值不是 plans 的属性名称。例如,您正在将其初始化为 "free"plans没有这样的属性。 plans["free"] undefined,因此您"无法读取未定义的属性'basePrice'">