Madurai
Madurai

Reputation: 297

How to update chrome.storage.local key and value?

I tried this code to update but i'm not need like this code. I need some API to update key and value.

Javascript:

var storage = chrome.storage.local;
       storage.get('key1', function (items) {        
       if(items.obj!="")
           {
           storage.remove('key1',function(response)
            {
               console.log(response);
            });
           }
       else
           {
           storage.set({'key1':'value1'},function(update)
                   {
               console.log(update);
                   });

           }

       });

Thank's in Advance.

Upvotes: 1

Views: 3265

Answers (1)

Armand Grillet
Armand Grillet

Reputation: 3399

I updated your code and this version worked:

var storage = chrome.storage.local;

storage.get("key1", function (items){        
    if(items.key1 != undefined) { // Or items["key1"] != undefined
       storage.remove("key1", function (){
           console.log("Key1 has been removed");
       });
    }
    else {
        storage.set({"key1":"value1"}, function (){
            console.log("Key1 has been set");
        });
    }
});

Don't forget to add "permissions": ["storage"] in your manifest.json (it looks like you did it, but this is a common oversight).

Upvotes: 0

Related Questions