PS D:\extag> node test.js
(node:16484) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined
at Cursor.exec (D:\extag\node_modules\nedb\lib\cursor.js:198:20)
at Datastore.findOne (D:\extag\node_modules\nedb\lib\datastore.js:526:12)
at internal/util.js:297:30
at new Promise ()
at internal/util.js:296:12
at main (D:\extag\test.js:10:23)
at Object. (D:\extag\test.js:4:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
(Use node --trace-warnings ... to show where the warning was created)
(node:16484) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:16484) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Additional information
In these two cases below, it will run correctly:
Manually promisify
const Datastore = require('nedb')
function findOne(arg) {
return new Promise(resolve=>{
db.findOne(arg, (err, res)=>resolve(res))
})
}
async function main() {
const db = new Datastore({filename:'test.db', autoload:true})
db.insert({'some':'thing'})
const res = await findOne({})
console.log(res)
}
main()
Use bluebird.Promise.promisify() instead
const { promisify } = require('bluebird')
const Datastore = require('nedb')
async function main() {
const db = new Datastore({filename:'test.db', autoload:true})
db.insert({'some':'thing'})
const findOne = promisify(db.findOne, {context:db})
const res = await findOne({})
console.log(res)
}
main()
What steps will reproduce the bug?
Seems the promisify(db.findOne) doesn't behave correctly.
How often does it reproduce? Is there a required condition?
always
What is the expected behavior?
What do you see instead?
Additional information
In these two cases below, it will run correctly:
Manually promisify
Use bluebird.Promise.promisify() instead