【问题标题】:Why am I getting this error 'TypeError undefined is not an object...' when including Shopify Linked Product Options?为什么我在包含 Shopify 链接产品选项时收到此错误“TypeError undefined is not an object...”?
【发布时间】:2015-05-18 14:31:07
【问题描述】:

我正在尝试在我的商店中实施 Shopify 的链接产品选项(遵循此 tutorial),它们似乎在产品页面上运行良好,但在我的索引和集合页面。

我在 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)');
  }

【问题讨论】:

    标签: javascript jquery shopify


    【解决方案1】:

    第 1 步:查找错误

    这意味着availableOptions是undefind所以Shopify.optionsMap[key]返回未定义。

    你应该试试这个来调试。是否定义了键?如果 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)');
      }
    

    【讨论】:

    • 您好,感谢您的回复。你介意解释一下我需要做什么来调试吗,这对我来说都是全新的。我将它放入 Safari 控制台,它返回以下内容:ReferenceError: Can't find variable: key 这是什么意思?
    • 我已经用 js 的一部分更新了我的问题,我确信它正在定义变量 key。这是否意味着它找不到.single-option-selector
    • 哦,我知道了!密钥仅在您的开关中定义。使 key 变量更具全局性。所以在开关外面定义 var 键,让key = 'root'; 和其他人。您还应该为您的开关添加默认情况!
    • 感谢您尝试实现它,但没有任何改变其他 js 仍然被破坏。任何想法为什么?看看所有的 JS 会有所帮助吗?再次感谢。
    • 也仍然得到相同的 js 错误。如果同一个脚本在一个页面上完美运行,但在站点的另一个页面上却不能正常运行,它怎么可能是未定义的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 2020-08-15
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多