javascript - Checking whether data already exists in chrome.storage.sync - Stack Overflow

I have a function that gets data from chrome.storage and loads it into an array, everything works fine

I have a function that gets data from chrome.storage and loads it into an array, everything works fine if I save an empty array into chrome.storage using a line of code then taking it out before using the extension as a user would.

The problem is that a user couldn't do that, if I start the extension for the first time without doing this then it looks like chrome passes back undefined into the variable which causes this error:

"Uncaught TypeError: Cannot read property '8' of undefined"

at this line:

if (SavedReminders[i][8] == "Full") {

I'm not entirely sure what exactly is going on, but I guess that if there's a way to check if the data exists already in chrome.storage or a way of saving an empty array just the first time the user opens the extension that would fix it.

Here's my Load function:

function LoadFromFile() {
    reminds = {}
    chrome.storage.sync.get({ reminds: [] }, function (result) { SavedReminders = result.reminds });
}

and save function:

function SaveToFile() {
    reminds={}
    chrome.storage.sync.set({reminds:SavedReminders }, 
        function () {
            console.log(SavedReminders, reminds);
        });
}

any help would be appreciated, thanks.

I have a function that gets data from chrome.storage and loads it into an array, everything works fine if I save an empty array into chrome.storage using a line of code then taking it out before using the extension as a user would.

The problem is that a user couldn't do that, if I start the extension for the first time without doing this then it looks like chrome passes back undefined into the variable which causes this error:

"Uncaught TypeError: Cannot read property '8' of undefined"

at this line:

if (SavedReminders[i][8] == "Full") {

I'm not entirely sure what exactly is going on, but I guess that if there's a way to check if the data exists already in chrome.storage or a way of saving an empty array just the first time the user opens the extension that would fix it.

Here's my Load function:

function LoadFromFile() {
    reminds = {}
    chrome.storage.sync.get({ reminds: [] }, function (result) { SavedReminders = result.reminds });
}

and save function:

function SaveToFile() {
    reminds={}
    chrome.storage.sync.set({reminds:SavedReminders }, 
        function () {
            console.log(SavedReminders, reminds);
        });
}

any help would be appreciated, thanks.

Share Improve this question asked Apr 1, 2015 at 19:34 JackJack 3876 silver badges27 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can pass an object to the api (instead of an array of keys) with default values for each key. It will return the default value for keys that arent there.
you can also just leave it as is and check if its ==undefined but my suggested way is cleaner.
details about how to pass such object are on the official api docs for .storage:

https://developer.chrome./extensions/storage StorageArea methods:get StorageArea.get(string or array of string or object keys, function callback) (optional) keys: ..."or a dictionary specifying default values"...

so chrome.storage.local.get({foo:"hi", bar:"there"}) will return obj where obj["foo"] is "hi" when not yet saved.

In my case, I was looking for a dictionary and the call seemed to be returning some kind of prototype object along with the dictionary (so the object type wasn't "undefined"). To get the value I was actually looking for, I had to check the property of the returned object.

chrome.storage.sync.get('profile', function(profileObj) {
  var profile = profileObj.profile;
  if (typeof profile === "undefined") {
    // No profile in storage
  } else {
    // Profile exists in storage
  }
});

Also you can check if storage.sync or storage.local returns an uninitialized storage using empty object detection:

function _empty(o) {
    return Object.keys(o).length === 0 && o.constructor === Object
}

So the line if (SavedReminders[i][8] == "Full") { will bee either if(!_empty(SavedReminders)) or if (SavedReminders && SavedReminders[i] && SavedReminders[i][8] == "Full") {.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745246504a4618434.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信