Level/leveldown

Not able to get put data

Closed this issue · 1 comments

import leveldown from 'leveldown'; let p = console.log // tsnd --respawn Test.ts
const db = leveldown('./aa')
db.open((e) => {
p(e)
})
db.put(key, 'value', function (a) {
console.log(a)
})
db.get(key, function (a) {
console.log(a)
})
db.close(function (a) {
console.log(a)
})

This doesn't work because you're not waiting for db.open() to complete before the put() call. Similarly, you're not waiting for put() to complete before the get() call.

It's recommended to use level because it will handle this for you, and doesn't require an explicit open() call:

const level = require('level')
const db = level('./aa')

await db.put('key', 'value')
const value = await db.get('key')

See also the readme:

It is strongly recommended that you use levelup in preference to leveldown unless you have measurable performance reasons to do so. levelup is optimised for usability and safety. Although we are working to improve the safety of the leveldown interface it is still easy to crash your Node process if you don't do things in just the right way.