我的場景是我正在嘗試將標題添加到節點請求對象。但是我只是想創建標題數組。創建對象時爲什麼會出現意外的令牌錯誤?
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