# Test for non-existing item in storage.local API

**URL:** <https://discourse.mozilla.org/t/test-for-non-existing-item-in-storage-local-api/9648>\
**Category:** Add-on Support\
**Created:** [July 9, 2016, 5:35pm UTC](https://discourse.mozilla.org/t/test-for-non-existing-item-in-storage-local-api/9648 "2016-07-09T17:35:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![h4ever](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@h4ever](https://discourse.mozilla.org/u/h4ever)\
**Post date:** [July 9, 2016, 5:35pm UTC](https://discourse.mozilla.org/t/test-for-non-existing-item-in-storage-local-api/9648/1 "2016-07-09T17:35:57Z")

</div>

When I run this function

function loadProfiles() {  
chrome.storage.local.set({advanced: data.advanced });  
chrome.storage.local.get(‘advanced’, gotItem);  
}

the scrit works with no problems.

When I run this

function loadProfiles() {  
chrome.storage.local.get(‘advanced’, gotItem);  
}

the script halts in debugger (It does not continue to run or execute the rest of the code). The item “advanced” does not exist yet and I need to find a way how to do a test if the item “advanced” exist in the database. This way as it is bug and the script will finish early.

---

<div class="post-metadata">

**Author:** ![jorgev](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jorgev](https://discourse.mozilla.org/u/jorgev)\
**Post date:** [July 12, 2016, 6:19pm UTC](https://discourse.mozilla.org/t/test-for-non-existing-item-in-storage-local-api/9648/2 "2016-07-12T18:19:58Z")

</div>

Are you saying that the `get` call throws an exception? Do you see anything reported in the Browser Console?

---

<div class="post-metadata">

**Author:** ![h4ever](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@h4ever](https://discourse.mozilla.org/u/h4ever)\
**Post date:** [July 12, 2016, 9:20pm UTC](https://discourse.mozilla.org/t/test-for-non-existing-item-in-storage-local-api/9648/3 "2016-07-12T21:20:03Z")

</div>

Sometimes I do some mistakes in code like in the case here and in that case I don’t see any messages in console. It just stopped working. Yet I do not use exceptions, so maybe I could use try … catch … to prevent console to stopped working? I did not tried yet. I forgot I could do it.

I have found a way how to test non-existing item in storage…

```
function checkIfExists(item) {
  if (chrome.runtime.lastError) {
    console.log("error:" + chrome.runtime.lastError);
    console.log(chrome.runtime.lastError);
  } else {
    if ( ! ( "initiated" in item ) )
      console.log("Not initiated");

    if ( ! ( "advanced" in item ) )
      {
      /* Insert new data object */
      chrome.storage.local.set(data, () => { 
        if (chrome.runtime.lastError) { console.log("local.set Error: " + chrome.runtime.lastError); } 
        chrome.storage.local.set( {initiated:true} );
        } );
      }
      else
        loadData()
  }
}
```
