出于某种原因,截至昨天我根本无法让 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 中...我第一次能够使某些东西工作。是的,它起作用了。我无法想象问题会是什么,尤其是当您使用存储宽变量时。
最佳答案
您需要将全局变量括在引号中:
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
错误,因为它只需要一组选定的字符(例如“+”或换行符)而不是连续字符串的字符。
这里的异常(exception)是全局计算结果为数字。在这种情况下,不需要引号,也不建议使用引号(类型冲突)。值得注意这一点,因为您在条件语句之一中使用了 ===
比较运算符,它会检查 type
(int, string等) AND 值
。因此,如果您尝试在字符串和数字之间使用 ===
,您的条件将会失败。
示例:
/* "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/