Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未处理的PromisejectionWarning:TypeError:dba.run不是函数_Javascript_Node.js_Sqlite_Asynchronous - Fatal编程技术网

Javascript 未处理的PromisejectionWarning:TypeError:dba.run不是函数

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 =&

我正在使用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 => {
    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');
                             ^