2021年1月1日星期五

UnhandledPromiseRejectionWarning: TypeError: dba.run is not a function

enter image description here

I'm working with node and sqlite, trying to connect asynchronously to the db with the sqlite-async node module(https://github.com/fhellwig/sqlite-async/blob/master/sqlite-async.js).

I have the following functions:

const Database = require('sqlite-async');    async function asyncdb() {  const dba = new Database;  return await dba.open('./test.db').then(() =>      console.log('async db opened')    )    .catch(err => {      console.log('async db failed');      })  

}

and:

async function select_from_table(dba,tablename, limit) {      let arr = [];    let sql = `SELECT Id FROM ${tablename} WHERE Type='Real' LIMIT ${limit}`;    return await dba.run(sql);  }  

When I run them as in the screenshot:

$ node sqlite.js     (node:12943) UnhandledPromiseRejectionWarning: TypeError: dba.run is not a function  at select_from_table (/home/optionhomes11/nodeprojects/hayes/sqlite.js:113:20)  at Object.<anonymous> (/home/optionhomes11/nodeprojects/hayes/sqlite.js:141:1)  at Module._compile (internal/modules/cjs/loader.js:999:30)  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)  at Module.load (internal/modules/cjs/loader.js:863:32)  at Function.Module._load (internal/modules/cjs/loader.js:708:14)  at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)  at internal/main/run_main_module.js:17:47  (node:12943) 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   

How can I fix this?

edit:

changing code to:

const dba = asyncdb();  console.log('out',dba);  

gives:

out Promise { <pending> }  async db opened  

edit2:

Thank you, but I'm getting syntax errors that say

console.log('async db opened');                               ^  

SyntaxError: missing ) after argument list

with your version of the function. Also I tried await and it would not allow it there saying it had to be in async function

enter image description here

https://stackoverflow.com/questions/65535409/unhandledpromiserejectionwarning-typeerror-dba-run-is-not-a-function January 02, 2021 at 09:59AM

没有评论:

发表评论