由于某种原因,截至昨天我根本无法使BC全局变量正常工作。他们不会返回任何内容,而是只会吐出错误,主要是下面第5行之后的内容。例如:

console.log(%%GLOBAL_CustomerGroupId%%);  //returns only errors
console.log(%%GLOBAL_StoreName%%);  //returns only errors
console.log("hello"); //returns "hello" (as it should)

OUTPUT - Uncaught SyntaxError: Unexpected token %


我尝试将代码直接放在几个不同页面的正文中(在脚本标签中),并且还尝试将代码放入普通的.js文档中。

我已经尝试过简单的console.logs和简单的条件语句,但是我无法使变量A.停止导致错误并B.返回任何内容

1|  if ( %%GLOBAL_CustomerGroupId%% === 3 ) {
2|     console.log("you are three");
3|     } else {
4|     console.log("you are not 3");
5|    }

OUTPUT - Uncaught SyntaxError: Unexpected token %  (for line 1)


我也几次收到错误消息,提示它无法识别“ ===”或“ =”。 (总是在if语句中谈论严格相等)

有任何想法吗?最近几天有什么变化吗?我从未遇到过BC全局变量的问题,现在我无法获得一个返回任何值的变量。谢谢你的时间。

编辑:

基于Alyss的评论,然后我尝试了以下操作:

var anotherBcGlobalTestingOfVariab = %%GLOBAL_StoreName%%;

  console.log("----store name below------");
  console.log(anotherBcGlobalTestingOfVariab);
  console.log("----store name above------");

RESULT: Uncaught SyntaxError: Unexpected token ;


删除分号,更改BC变量:

var anotherBcGlobalTestingOfVariab = %%GLOBAL_CustomerName%%

  console.log("----customer name below------");
  console.log(anotherBcGlobalTestingOfVariab);
  console.log("----customer name above------");

RESULT:
----customer name below------
undefined
----customer name above------


当我将变量设置为%% GLOBAL_StoreName %%且不使用分号时,会发生有趣的事情,与上面的示例相同,但BC变量不同:

  var bcGlobalTestingOfVariab = %%GLOBAL_StoreName%%;

    console.log("----store name below------");
    console.log(bcGlobalTestingOfVariab);
    console.log("----store name above------");

  Uncaught ReferenceError: CENSORED is not defined


CENSORED是商店的名称,因此它以某种方式返回了商店名称,但是存在错误。我尝试了其他几个具有相同结果的BC变量。

第二编辑:

if (%%GLOBAL_CustomerGroupId%% === 9) {
  console.log("congrats, it only took you 20 hours");
} else {
  console.log("you are not a nine");
}


放在脚本标签底部的default.html中...第一次可以使某些功能生效。是的。我无法想象问题出在哪里,尤其是当您使用存储范围变量时。

最佳答案

您需要将Globals括在引号中:

var a = "%%GLOBAL_Example%%";
console.log("%%GLOBAL_Example%%");




这些全局变量由模板引擎(php)评估,并已被评估后发送到浏览器(客户端)。例如,如果%%GLOBAL_Example%%评估为Some Example String,那么请看一下当未用引号引起来时,JavaScript解释器的外观:

var a = Some Example String;
console.log(Some Example String);


现在,这里的语法错误应该很明显,您可以查看页面源来直接查看这些全局变量的显示方式。由于没有引号,因此JS解释器认为您正在引用变量,因此在解析第一个单词后,它会失败并显示Unexpected Token错误,因为它只希望选择一组字符(例如'+'或换行),而不是连续字符串的字符。



唯一的例外是,如果Global求一个数字。在这种情况下,不需要引号,也不建议使用引号(类型冲突)。请注意这一点,因为您在其中一个条件语句中使用了===比较运算符,该条件运算符检查type(整数,字符串等)和value中的等效性。因此,如果您尝试在字符串和数字之间使用===,则条件将失败。

例:

/* "9" is a string, whereas 9 (without quotes) is a number  */
console.log("9" === 9 ? 'Equal' : 'Not Equal!'); //Prints 'Not Equal!'
console.log(9   === 9 ? 'Equal' : 'Not Equal!'); //Prints 'Equal'


最后一点,分号在JavaScript中是完全可选的。

关于javascript - 我无法继续使用Bigcommerce全局变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37444439/

10-12 12:31
查看更多