turoni
turoni

Reputation: 1435

Angular frontend syntax error

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

Upvotes: 0

Views: 248

Answers (3)

turoni
turoni

Reputation: 1435

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.

Upvotes: 0

Miroslav Jonas
Miroslav Jonas

Reputation: 6637

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.

Upvotes: 1

toskv
toskv

Reputation: 31612

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.

Upvotes: 0

Related Questions