私のシナリオでは、ノード要求オブジェクトにヘッダーを追加しようとしています。しかし、ヘッダー配列を作成するだけで忙しかったです。オブジェクトの作成中に予期しないトークンエラーが発生するのはなぜですか?
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'を本当に使いたいなら、quote:' 'const.headers.authToken" ' –
の間で囲む必要があります。実際のコードではconstではありません。私は、別のバグを導入することを気付かないうちに、難読化するための質問に変更しました。私の悪い。私は質問を更新します。 – noctonura