Javascript 未处理的PromisejectionWarning:TypeError:dba.run不是函数
我正在使用node和sqlite,尝试使用sqlite异步节点模块()异步连接到db 我有以下职能:Javascript 未处理的PromisejectionWarning:TypeError:dba.run不是函数,javascript,node.js,sqlite,asynchronous,Javascript,Node.js,Sqlite,Asynchronous,我正在使用node和sqlite,尝试使用sqlite异步节点模块()异步连接到db 我有以下职能: 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 =&
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');
})
}
以及:
当我像截图一样运行它们时:
$ 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
给出:
out Promise { <pending> }
async db opened
SyntaxError:在参数列表之后缺少)
使用您的函数版本。我还尝试了wait,但它不允许它出现在那里,说它必须在异步函数中
您没有在
上处理返回的数据库。然后()
根据
我认为下面的方法应该有效
const Database=require('sqlite-async');
异步函数asyncdb(){
const dba=新数据库;
返回等待dba.open('./test.db')。然后((db)=>{
log('async db opened');
返回分贝;
})
.catch(错误=>{
log('async db failed');
})
}
调用asyncdb
函数时,您也不会等待它
const dba=await asyncdb();
console.log('out',dba);
同样值得:asyndb中的catch
处理程序应使用throw err抛出其参数代码>语句,而不是返回<默认情况下,code>catch
处理程序不会将错误条件传播到承诺链中。dba
的值仅在asyncdb
返回的挂起承诺得到满足后才可用。这意味着可以从async
函数中调用asyncdb
,以便在其返回值上使用wait
运算符,也可以从添加到挂起承诺的处理程序中调用asyncdb
。
const dba = asyncdb();
console.log('out',dba);
out Promise { <pending> }
async db opened
console.log('async db opened');
^