我无法再让Bigcommerce全局变量工作

时间:2016-05-25 17:54:41

标签: javascript global-variables bigcommerce

出于某种原因,我不能让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中......第一次我能够做出一些有用的东西。是的,它工作。我无法想象在使用商店范围的变量时会出现什么问题。

1 个答案:

答案 0 :(得分:1)

您需要将Globals包装在引号中

root.GetFiles("*.dll", SearchOption.AllDirectories);

这些Globals由模板引擎(php)评估,并在已经评估时发送到浏览器(客户端)。例如,如果var a = "%%GLOBAL_Example%%"; console.log("%%GLOBAL_Example%%"); 的计算结果为%%GLOBAL_Example%%,那么请查看JavaScript解释器在未包含在引号内时的显示方式:

Some Example String

此处的语法错误现在应该是显而易见的,您可以查看页面源来直接查看这些Globals的显示方式。由于没有引号,JS解释器认为你是指一个变量,所以在它解析第一个单词之后,它会因var a = Some Example String; console.log(Some Example String); 错误而失败,因为它只期望一组精选的字符(例如' +'或新行)而不是继续字符串的字符。

此处的例外情况是全局评估数字。在这种情况下,不需要引号,不建议使用(类型冲突)。这一点很重要,因为您在一个条件语句中使用Unexpected Token比较运算符,它检查=== (int,string等)中的等效性 < strong> AND type。因此,如果您尝试在字符串和数字之间使用value,则您的条件将失败。

示例

===

最后请注意,分号在JavaScript中是完全可选的。