Test for non-existing item in storage.local API

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.

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

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()
  }
}