为什么我得到这个错误'TypeError undefined不是一个对象…'包括Shopify链接产品选

Why am I getting this error 'TypeError undefined is not an object...' when including Shopify Linked Product Options?

本文关键字:一个对象 包括 Shopify 链接 TypeError undefined 为什么 错误      更新时间:2023-09-26

我正试图实现Shopify的链接产品选项到我的商店(遵循本教程),他们似乎在产品页面上工作得很好,但打破了其他脚本(购物车&在我的索引和收藏页面上的快速商店打开但没有内容。

我在Safari中得到以下错误:

TypeError: undefined is not an object (evaluating 'availableOptions.length')

引用这段代码:

var initialValue = selector.val();
  selector.empty();    
  var availableOptions = Shopify.optionsMap[key];
  for (var i=0; i<availableOptions.length; i++) {
    var option = availableOptions[i];
    var newOption = jQuery('<option></option>').val(option).html(option);
    selector.append(newOption);
  }

如果有人对这可能是为什么有任何建议,我将感谢您的输入。我对JS不是很好,所以在这个问题上挣扎!

更新:

我很确定这部分是定义'键' -如果键是未定义的,这是否意味着它不能找到.single-option-selector ?

 switch (selectorIndex) {
case 0:
  var key = 'root';
  var selector = jQuery('.single-option-selector:eq(0)');
  break;
case 1:
  var key = jQuery('.single-option-selector:eq(0)').val();
  var selector = jQuery('.single-option-selector:eq(1)');
  break;
case 2:
  var key = jQuery('.single-option-selector:eq(0)').val();  
  key += ' / ' + jQuery('.single-option-selector:eq(1)').val();
  var selector = jQuery('.single-option-selector:eq(2)');
  }

步骤1:查找错误

表示availableOptions是undefind,所以Shopify.optionsMap[key]返回undefined

你应该试着调试一下。是否定义了键?如果Shopify是对的?Shopify。optionsMap存在吗?

console.log(key);
console.log(Shopify);
console.log(Shopify.optionsMap);
console.log(Shopify.optionsMap[key]);

步骤2:使它更全局

var key = '';
var selector = '';
switch (selectorIndex) {
case 0:
  key = 'root';
  selector = jQuery('.single-option-selector:eq(0)');
  break;
case 1:
  key = jQuery('.single-option-selector:eq(0)').val();
  selector = jQuery('.single-option-selector:eq(1)');
  break;
case 2:
  key = jQuery('.single-option-selector:eq(0)').val();  
  key += ' / ' + jQuery('.single-option-selector:eq(1)').val();
  selector = jQuery('.single-option-selector:eq(2)');
  }