Home > Software engineering >  Getting SyntaxError: Unexpected token in JavaScript
Getting SyntaxError: Unexpected token in JavaScript

Time:02-13

I was trying vega-lite-api on observablehq here.

This is my code:

Putting these two in different cells work:

obj = vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'));

In next cell:

obj.render()

If I put both in the same cell, it gives SyntaxError: Unexpected token:

obj2 = vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'));

obj2.render();
^

Why is this so?

CodePudding user response:

for obj you have a definition before you use it...

obj = N {
  Symbol(data): Object {mark: h, data: Object, encoding: h}
  <prototype>: N {}
}

I guess you forgot about it for obj2?

CodePudding user response:

If you want to put the code into the same cell, you can either chain the call to render like so:

vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'))
  .render()

Or you can use a block cell with curly braces like so:

{
  const obj = vl.markPoint()
    .data(df)
    .encode(vl.y().fieldN('city'));

  return obj.render();
}

For more on Observable JavaScript:

https://observablehq.com/@observablehq/observables-not-javascript

  • Related