0

When I start my angular frontend I get the following syntax error

choose.html:1 Uncaught SyntaxError: Unexpected token l in JSON at position 1
at JSON.parse ()
at Object.fromJson (vendor.js:14110)
at Object.getObject (vendor.js:4234)
at Object.get (vendor.js:4347)
at Object.get (vendor.js:47560)
at runTranslate (vendor.js:47789)
at Object.invoke (vendor.js:17454)
at vendor.js:17262
at forEach (vendor.js:13111)
at createInjector (vendor.js:17262)

The vendor.js is automatically created and when I look up the error it is really specific to what people wrote and not something auto generated. Any idea in which direction I need to look:

At 14110 is the following segment:

/**
 * @ngdoc function
 * @name angular.fromJson
 * @module ng
 * @kind function
 *
 * @description
 * Deserializes a JSON string.
 *
 * @param {string} json JSON string to deserialize.
 * @returns {Object|Array|string|number} Deserialized JSON string.
 */
function fromJson(json) {
  return isString(json)
      ? JSON.parse(json)
      : json;
}
3
  • JSON.stringify(json); instead of JSON.parse(json) Commented Jun 10, 2017 at 15:50
  • All code in vendor.js is generated after I build my angular application with gulp so I don't know how to change this. Commented Jun 10, 2017 at 15:52
  • have a look at my answer, is there anything in your build script (some gulp plugin) that could do that? Commented Jun 10, 2017 at 16:58

3 Answers 3

1

Problem is not in vendor.js nor in way the vendor.js is compiled. It says Undexpected token I in JSON at position 1. That means your application is trying to parse JSON object (with above mentioned function fromJson and fails as it stumbles upon invalid JSON string. Put breakpoint on the function and re-run the app to see which JSON exactly crashes. Hopefully you'll figure out then what you did wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

You can use Chrome Dev Tools or Firebug for example. If you click on the right side next to your console error report (something like vendor.js:x) it will take you right to the function fromJson. Just place breakpoint there and refresh the page.
0

This might be because the compiler or something else inserts ; at the end of the return statement.

Thus transforming your into something like this which is invalid javascript.

function fromJson(json) {
  return isString(json);  // added ; here
      ? JSON.parse(json) // no longer valid javascript
      : json;
}

Make sure the code is not transformed this way, or write the ternary expression on a single line.

2 Comments

I don't think my gulp buildscript does that and the code I copies is the live version in the chrome developper tool so I'm pretty sure there is no ; there
and the version you run is not minified in any way?
0

I have not really been able to confirm it and it is not a real solution but the line

at runTranslate (vendor.js:47789)

led me to believe there was a fault in my translate app. Because I did not need it I removed it from my application and it indeed solved my problems. This of course is not a true fix but it was enough for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.