我的场景是我正在尝试将标题添加到节点请求对象。但是我只是想创建标题数组。创建对象时为什么会出现意外的令牌错误?
let headers = [];
let options = {
url: this.baseUrl + appconst.route.postMessage
};
if (token) {
console.log(appconst.headers.authToken); // no problem here
let authTokenHeader = { appconst.headers.authToken: token.toBase64() }; // throws
headers.push(authTokenHeader);
}
if (msg) {
headers.push({ 'content-type': 'application/json' });
options.body = JSON.stringify(msg);
}
的“抛出”线给...
let authTokenHeader = { appconst.headers.authToken: token.toBase64() };
^
SyntaxError: Unexpected token .
能否请你帮我明白是怎么回事?我很困惑,因为定义了“const.headers.authToken”,并且它上面的代码行很好。是的,我对JavaScript很陌生。
您不应该使用'const'作为变量名称。 –
如果你真的想使用'const.headers.authToken'作为你的对象的键名,你需要将它包装在''const.headers.authToken“' –
对不起。它在实际代码中不是常量。我只是改变了它的问题来混淆没有意识到引入另一个错误。我的错。我会更新这个问题。 – noctonura