How to check if key is not found in IndexedDB?

On the indexeddb i want to look if there is a key permanent and do some actions. But if not, i want to make some other actions. I can do the actions if the permanent is there, however when it is not I can get the onerror to work. Is the onerror suppose to do this thing? How can I check if there is not value in it?

var hashtype='permanent';
    var getPermanent = store.get(hashtype);

getPermanent.onsuccess = function() {
    var ivrame=getPermanent.result.value;
};

getPermanent.onerror = function() {
  console.log('onerror')
};

  • Show video blob after reading it through AJAX
  • Inconsistent interplay between IndexedDB transactions and Promises
  • IndexedDB: Query index where value “contains”
  • Encryption of sensitive data on client side
  • Detect Firefox IndexedDB or Web Storage storage limit, without filling up the disk?
  • Invalid key path in IndexedDB: restrictions?
  • move profile folder to sdcard firefox mobile 14.0
  • Storing Image Data for offline web application (client-side storage database)
  • Is there any way to retrieve random row from indexeddb
  • Cannot open IndexedDB in firefox
  • How to persist data in a Service Worker
  • InvalidStateError while opening IndexedDB in Firefox
  • 2 Solutions collect form web for “How to check if key is not found in IndexedDB?”

    See the note under https://w3c.github.io/IndexedDB/#dom-idbobjectstore-get – the get method yields success with undefined if there is no matching record.

    So you have a few options:

    • Use get(key) and test the result for undefined. This works unless undefined is a value you expect to store (it’s a valid value)
    • Use count(key) – the result will be 1 if present, 0 if absent. Easy if you’re just testing for existence, but doesn’t get you the record.
    • Use openCursor(key) and test to see if the request’s result is a cursor (record present as request.result.value) or undefined (no record in range)

    For your code:

    var hashtype='permanent';
    
    // #1: Use get
    var getPermanent = store.get(hashtype);
    getPermanent.onsuccess = function() {
        if (getPermanent.result === undefined) {
            // no record with that key
        } else {
            var value = getPermanent.result;
        }
    };
    
    // #2: Use count
    var getPermanent = store.count(hashtype);
    getPermanent.onsuccess = function() {
        if (getPermanent.result === 0) {
            // no record with that key
        } else {
            ...
        }
    };
    
    // #3: Use cursor
    var getPermanent = store.openCursor(hashtype);
    getPermanent.onsuccess = function() {
        var cursor = getPermanent.result;
        if (!cursor) {
            // no record with that key
        } else {
            var value = cursor.value;
        }
    };
    

    The function assigned to request.onsuccess is a callback function that is always called, regardless of whether the value is present in the store. When there is no corresponding object in the store, the result object will be undefined. When there is a corresponding object in the store, the result object will be defined. So you simply need to check if the object is defined from within the onsuccess callback function.

    request.onerror is a separate callback from request.onsuccess. onerror gets called when there is some type of failure in indexedDB (e.g. something like you tried to get a value from a store that doesn’t exist, or you tried to put a duplicate object into a store that doesn’t permit duplicates). request.onerror does not get called when no value is found as a result of calling store.get, because that is not considered an ‘error’ in the failure sense.

    So, what you want to do is something like this:

    var hashtype='permanent';
    var getPermanent = store.get(hashtype);
    
    getPermanent.onsuccess = function(event) {
      //var ivrame=getPermanent.result.value;
    
      var result = getPermanent.result;
      if(result) {
        console.log('Got a result!', result);
        var ivrame = result;
      } else {
        console.log('Result was undefined! No matching object found');
      }
    };
    
    getPermanent.onerror = function() {
      console.log('Something went wrong trying to perform the get request');
    };
    

    Do not try and access request.result.value. There is no such thing in the case of a get request. When using store.get, request.result contains the matching object you want, or is undefined. When using store.openCursor, request.result contains the cursor, which is defined if there is at least one matching object and you have not already iterated past it. To get the matching object at the cursor’s current position, you would use cursor.value. Here, cursor.value will always be defined, because cursor would otherwise be undefined, and you would obviously check for that beforehand.