Why is there an unexpected token error when creating object? [duplicate]

272
May 29, 2017, at 02:28 AM

This question already has an answer here:

  • Using a variable for a key in a JavaScript object literal 5 answers

My scenario is that I am trying to add headers to a node request object. But I'm hung up on just creating the headers array.

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);
}

The 'throws' line is giving...

  let authTokenHeader = { appconst.headers.authToken: token.toBase64() };
                                  ^
  SyntaxError: Unexpected token .

Can you please help me understand what is going on here? I am very confused because the "const.headers.authToken" is defined and the line above it works fine. Yes, I am very new to JavaScript.

Answer 1

const - is reserved keyword. Simply pick another one for your needs.

READ ALSO
Amazon S3 403 Forbidden Error for KML files but not JPG files

Amazon S3 403 Forbidden Error for KML files but not JPG files

I am able to successfully upload (put object) jpg files to S3 with a particular code path, but receive a 403 forbidden error when using the same code path to upload a KML fileI am not restricting file types explicitly with "bucket policy," but feel that this must somehow...

349
Express passport middleware not woriking with get request params

Express passport middleware not woriking with get request params

I'm building a webserver with node, express, jwt and passportBut when I try to have a route with a get methode:

318
How to create a Node js server which can allow connection from any device connected to my home network?

How to create a Node js server which can allow connection from any device connected to my home network?

I am a NewbieI searched many websites for same but couldn't find any appropriate solution

278