Level / community

Discussion, support and common information for projects in the community.
MIT License
42 stars 15 forks source link

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

Closed kevinkyle4 closed 3 months ago

kevinkyle4 commented 6 months ago

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

vweevers commented 6 months ago

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')
  }
}
juliangruber commented 6 months ago

Something like this should work:

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

above solved my issue. thanks