{"id":6247,"date":"2014-04-14T03:22:40","date_gmt":"2014-04-14T03:22:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/saving-and-retrieving-from-chrome-storage-sync-collection-of-common-programming-errors\/"},"modified":"2014-04-14T03:22:40","modified_gmt":"2014-04-14T03:22:40","slug":"saving-and-retrieving-from-chrome-storage-sync-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/saving-and-retrieving-from-chrome-storage-sync-collection-of-common-programming-errors\/","title":{"rendered":"saving and retrieving from chrome.storage.sync-Collection of common programming errors"},"content":{"rendered":"<p>The problem is with <code>chrome.storage.sync.set({key: testPrefs}<\/code><\/p>\n<p>Your data is stored as<\/p>\n<pre><code>{\n    key: \"{\"val\":10}\"\n}\n<\/code><\/pre>\n<p>So, your code <code>chrome.storage.sync.get('myKey')<\/code> return <strong>undefined\\empty object<\/strong>.<\/p>\n<h2>Solution I<\/h2>\n<p>Use string <code>\"key\"<\/code> as your key<\/p>\n<pre><code>chrome.storage.sync.get(\"key\", function (obj) {\n    console.log(obj);\n});\n<\/code><\/pre>\n<p>or<\/p>\n<h2>Solution II<\/h2>\n<p>Set data through <code>\"myKey\"<\/code> Key.<\/p>\n<pre><code>chrome.storage.sync.set({\"myKey\": testPrefs}\n<\/code><\/pre>\n<p><strong>P.S<\/strong> : Don&#8217;t forget <code>chrome.storage.sync<\/code> is permanent storage API, Use chrome.storage.sync.clear before any further testing to see changes<\/p>\n<h2>References<\/h2>\n<p>Use this code to set variable value in Chrome.storage<\/p>\n<pre><code>function storeUserPrefs() {\n    var key = \"myKey\",\n        testPrefs = JSON.stringify({\n            'val': 10\n        });\n    var jsonfile = {};\n    jsonfile[key] = testPrefs;\n    chrome.storage.sync.set(jsonfile, function () {\n        console.log('Saved', key, testPrefs);\n    });\n\n}\n<\/code><\/pre>\n<p>It generates following Output<\/p>\n<pre><code>Object{\n    myKey: \"{\"val\":10}\"\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The problem is with chrome.storage.sync.set({key: testPrefs} Your data is stored as { key: &#8220;{&#8220;val&#8221;:10}&#8221; } So, your code chrome.storage.sync.get(&#8216;myKey&#8217;) return undefined\\empty object. Solution I Use string &#8220;key&#8221; as your key chrome.storage.sync.get(&#8220;key&#8221;, function (obj) { console.log(obj); }); or Solution II Set data through &#8220;myKey&#8221; Key. chrome.storage.sync.set({&#8220;myKey&#8221;: testPrefs} P.S : Don&#8217;t forget chrome.storage.sync is permanent storage API, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6247","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6247"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6247\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}