Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught SyntaxError: Unexpected token g in JSON at position 0 #722

Closed
1 of 6 tasks
f-santos opened this issue Jul 2, 2019 · 4 comments
Closed
1 of 6 tasks

Uncaught SyntaxError: Unexpected token g in JSON at position 0 #722

f-santos opened this issue Jul 2, 2019 · 4 comments

Comments

@f-santos
Copy link

f-santos commented Jul 2, 2019

Description

Once a new element attribute of type Array or Object is set but the value is a JavaScript object value added to the attribute with no .property notation the following exception is raised:
Uncaught SyntaxError: Unexpected token g in JSON at position 0

I discovered it because of a misconfiguration but as no clear exception is raised it took some time to discover what exactly was the issue.

Live Demo

https://lit-element-example-3c7ojq.stackblitz.io

Steps to Reproduce

  1. Create my-element with an Array type attribute and render it as follows
    render(html<my-element moods=${['a', 'b']}>, document.body);

Expected Results

As the set value is wrong I expect the attribute to be set to a initial value [] or undefined but no exception raised.

Actual Results

Console exception:
Uncaught SyntaxError: Unexpected token g in JSON at position 0

Browsers Affected

I did not checked all but I believe the same shall happen in all major browsers.

  • Chrome
  • Firefox
  • Edge
  • Safari 11
  • Safari 10
  • IE 11

Versions

  • lit-element: 2.1.0
@sorvell
Copy link
Member

sorvell commented Jul 9, 2019

That's fair, it should probably not throw an exception when an improper value is set. A PR would be welcome on this and would probably involve using a try/catch with a good fallback somewhere in here: https://github.com/Polymer/lit-element/blob/master/src/lib/updating-element.ts#L147

@f-santos
Copy link
Author

f-santos commented Jul 10, 2019

@sorvell I've provided a possible solution in PR #723

@benjaminParisel
Copy link

I resumed the work of @f-santos from PR #723 and opened #888 with test and return to fixed this issue

@sorvell sorvell self-assigned this Mar 3, 2020
sorvell pushed a commit to lit/lit that referenced this issue Sep 2, 2020
@justinfagnani
Copy link
Contributor

Fixed in Lit 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment