Reputation: 5
So I'm trying to save an object via the chrome.storage API. The relevant code is:
var images = null;
var storage = chrome.storage.sync;
var key = "key";
images = getImages(source);
alert(images.length); // returns 4
storage.set({key: images});
storage.get("key", function(result) {
alert(result.length); // returns undefined
});
I'm tested that immediately after the getImages()
function, images is a wrapped set JQuery object with a length of 4. However, when I try to access images.length
via the storage.get
callback, the result is undefined.
Could someone help identify the error in how I am storing and/or retrieving this JQuery object?
Thank you all for your help. As clarification for the use case, I am using chrome.storage
instead of localStorage
because I plan to pass extension info to another script.
Fortunately, TranQ/Xan's solution has enabled me to access the array via the storage.get
call.
I'm still experiencing issues working with the wrapped set JQuery object stored in the array but I'll post a separate question since the current solution encapsulates broader use cases.
Upvotes: 0
Views: 593
Reputation: 77591
TranQ's comment is on point.
Presumably, images
is an array. You store that array under the "key"
key.
When you execute the get()
function, it returns an object populated with all key-value pairs you asked, even if you only ask for one key.
So, result
is an object {key : [/* something */]}
. Objects do not have a length
property, and you get undefined
You need to use result.key
(or result["key"]
) to access your array.
Upvotes: 1