community icon indicating copy to clipboard operation
community copied to clipboard

Getting no entry found error in Chrome when using .get("key")

Open kevinkyle4 opened this issue 1 year ago • 2 comments

If I am trying to fetch a value by its key before the value exists in DB it throws a "Entry Not Found" error in browser which crashes app. How do I use .get() to return zero results if entry is not found like in MongoDB? Thanks

kevinkyle4 avatar Feb 29 '24 03:02 kevinkyle4

This is by design, but changing in abstract-level 2.0.0. Until then, do:

try {
  await db.get('example')
} catch (err) {
  if (err.code === 'LEVEL_NOT_FOUND') {
    console.log('Not found')
  }
}

vweevers avatar Feb 29 '24 09:02 vweevers

Something like this should work:

let value = null
try {
  value = await db.get('key')
} catch (err) {
  if (err.type !== 'NotFoundError') {
    throw err
  }
}

juliangruber avatar Feb 29 '24 09:02 juliangruber

above solved my issue. thanks

kevinkyle4 avatar Jun 07 '24 00:06 kevinkyle4