0
var storage = chrome.storage.local;
var cachedStorage = {};

this is js file.It shows unexpected token u.even though I've done parsing correctly.and it also shows unexpected token for for its html source page.can any one suggest me how to sort this out.


var defaultStorage = [{
  savedPatterns: JSON.stringify([
    [{
      "en": "English"
    }, {
      "it": "Italian"
    }, "25", true],
    [{
      "en": "English"
    }, {
      "la": "Latin"
    }, "15", false]
  ]),

}];

error occurs here unexpected token u

function createPattern() {
  console.log('createPattern begin');
  var patterns = JSON.parse(S('savedPatterns'));
  var srce = [],
    trg = [],
    prb = [];
  console.log(S('savedPatterns'));
  console.debug(S('savedPatterns'));
  var translator = document.getElementById('translatorService');
  var service = translator.children[translator.selectedIndex].value;
  srce[0] = document.getElementById('sourceLanguage');
  srce[1] = srce[0].children[srce[0].selectedIndex].value;
  srce[2] = srce[0].children[srce[0].selectedIndex].text;
  trg[0] = document.getElementById('targetLanguage');
  trg[1] = trg[0].children[trg[0].selectedIndex].value;
  trg[2] = trg[0].children[trg[0].selectedIndex].text;
  prb[0] = document.getElementById('translationProbability');
  prb[1] = prb[0].children[prb[0].selectedIndex].value;

  patterns.push([
    [srce[1], srce[2]],
    [trg[1], trg[2]],
    prb[1],
    false,
    service
  ]);
  saveBulk({
    'savedPatterns': JSON.stringify(patterns)
  }, 'Saved Pattern');
  console.log('createPattern end');
}

function S(key) {
  return cachedStorage[key];
}

function loadStorageAndUpdate(callback) {
  storage.get(null, function(data) {
    console.log('data: ' + data + ' : ' + JSON.stringify(data));
    var d = {};
    if (!data || JSON.stringify(data) == '{}') { // in this case, storage was not initialized yet
      console.log('setting storage to defaultStorage (stringified): ');
      console.log(JSON.stringify(defaultStorage));
      storage.set(defaultStorage);
      d = defaultStorage;
    } else {
      d = data;
    }

    cachedStorage = d;
    if (!!callback) {
      callback(d);
    }
  });
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79
Sriker Ch
  • 133
  • 11
  • The error is `cachedStorage[key]` is returning `undefined` for some key. I'd suggest, call function and save returned value in a variable and if variable has some value, try parsing it – Rajesh Mar 11 '16 at 07:01

2 Answers2

1

Error Unexpected token comes when JSON.parse fails and depending on character (u in this case), you can assume its cause.

  • u is if value is undefined
  • o is if value is object

try {
  JSON.parse(undefined)
} catch (ex) {
  document.write(ex.message + "<br/>")
}

try {
  JSON.parse({})
} catch (ex) {
  document.write(ex.message)
}

You can try something like this:

function s(key) {
  var obj = {
    foo: "foo",
    bar: "bar"
  }
  var v = null;

  try {
    v = JSON.parse(obj[key]);
  } catch () {
    v = obj[key];
  }

  return v;
}

function main() {
  var v = s("foo");
}

Please refer following post for more information. Uncaught SyntaxError: Unexpected token with JSON.parse

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

You're trying to parse undefined. When you call JSON.parse(), you're passing in S('savedPatterns'), which in turn tries to access cachedStorage['savedPatterns'], but that starts as undefined, which you can't parse. You could just initialize your cachedStorage as:

var cachedStorage = {
  savedPatterns: JSON.stringify([])
};
Matthew Brooks
  • 521
  • 2
  • 5
  • hey matthew thanks for that,But after this change also it is showing unexpected token u.for its source html page – Sriker Ch Mar 11 '16 at 08:20