Sunday, 28 May 2017

Why is there an unexpected token error when creating object?

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 + const.route.postMessage
};

if (token) {
  console.log(const.headers.authToken); // no problem here
  let authTokenHeader = { const.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 = { const.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.



via RichAmberale

No comments:

Post a Comment