【问题标题】:"This language feature is only supported for ECMASCRIPT6 mode or better: arrow function."“此语言功能仅支持 ECMASCRIPT6 或更高模式:箭头功能。”
【发布时间】:2021-01-31 08:57:12
【问题描述】:

我的代码在上传时遇到问题。我正在尝试在 GTM 中提交此代码,尽管它说“=>”不接受通过此线程的标题。我可以在制作这些正常功能方面获得帮助吗?错误出现在此部分“var items = products.map”中。

我们将不胜感激。

<script>
 window.addEventListener("message", receiveMessage, false);

  function receiveMessage(event) {
    var payload = event.data && event.data.payload;

    if (!payload || event.data.messageType !== 'analyticsEvent') return;

    if (
      payload.name === "checkout"
    ) {
      var customerEmail = 
        payload.properties && payload.properties.customerEmail;
      var subtotal = 
        payload.properties && payload.properties.estimatedTotal;
      var cartId =
        payload.properties && payload.properties.cartId;
      var products =
        payload.properties && payload.properties.products;

      // do something with payload          
      console.log(subtotal, cartId, products);
      
      // if you're using Enhanced Ecommerce in Google Analytics, 
      // you can use the following code:
      var items = products.map(({ product_id, name, brand, category, kind, unit_price, count, special_id, special_title }) => ({id: product_id, name: name, brand: brand, category: kind, variant: category, quantity: count, price: unit_price}))

      gtag('event', 'purchase', {
        "transaction_id": cartId,
        "affiliation": "store",
        "value": subtotal,
        "currency": "USD",
        "items": items
      });
    }
    
    if (
      payload.name === 'menuLoad' 
    ) {
       var customerEmail = 
         payload.properties && payload.properties.customerEmail;
         
       // do something with the customerEmail if user is authenticated
       console.log(customerEmail);
    }

    if (
      payload.name === 'productView' 
    ) {
       var productId = 
         payload.properties && payload.properties.productId;
       var productKind = 
         payload.properties && payload.properties.productKind;
       var product = 
         payload.properties && payload.properties.product;
         
       // do something with productId, productKind, product
       console.log(productId, productKind, product);
    }

    if (
      payload.name === 'cartItemAdd' 
    ) {
       var productId = 
         payload.properties && payload.properties.productId;
       var product = 
         payload.properties && payload.properties.product;

       console.log(productId); // do something with the productId 
    }

    if (
      payload.name === 'cartItemRemoval' 
    ) {
       var productId = 
         payload.properties && payload.properties.productId;
         
       console.log(productId); // do something with the productId
    }
  }
</script>

【问题讨论】:

  • 改用常规函数? () =&gt; {} 变为 function () {}
  • 使用 Laravel-mix。 (您不必从事 Laravel 项目)它是 JS 库。
  • 谢谢,喜欢这样吗? var items = products.map(function(product_id, name, brand, category, kind, unit_price, count, special_id, special_title) {id: product_id, name: name, brand: brand, category: kind, variant: category, quantity:计数,价格:unit_price})
  • 或许可以考虑在使用之前花点时间学习语言基础知识。
  • 我研究了基础知识,但不懂编码。我正在安装此代码以通过客户网站上的 iframe 菜单跟踪销售情况。我以前从未遇到过这种情况。

标签: javascript function


【解决方案1】:
var items = products.map(({ product_id, name, brand, category, kind, unit_price, count, special_id, special_title }) => ({id: product_id, name: name, brand: brand, category: kind, variant: category, quantity: count, price: unit_price}))

可能会变成这样:

var items = products.map(function(product) {
    return {
        id: product.product_id,
        name: product.name,
        brand: product.brand,
        category: product.kind,
        variant: product.category,
        quantity: product.count,
        price: product.unit_price
    };
});

因为 ES6 代码使用对象解构和隐式(?)返回值。

【讨论】:

  • 成功了!顺利通过了,非常感谢您的帮助!
猜你喜欢
  • 2019-09-03
  • 2021-05-17
  • 2021-12-29
  • 1970-01-01
  • 2021-05-08
  • 2017-05-27
  • 2011-01-24
  • 2016-05-06
  • 2016-05-05
相关资源
最近更新 更多