cod*_*321 8 javascript typeerror node.js
在以下代码中,出现此错误:
TypeError [ERR_INVALID_ARG_TYPE]:“原始”参数必须为Function类型。接收类型未定义
const sqlite3 = require('sqlite3').verbose();
const util = require('util');
async function getDB() {
return new Promise(function(resolve, reject) {
let db = new sqlite3.Database('./project.db', (err) => {
if (err) {
console.error(err.message);
reject(err)
} else {
console.log('Connected to the project database.');
resolve(db)
}
});
return db
});
}
try {
// run these statements once to set up the db
let db = getDB();
db.run(`CREATE TABLE services(id INTEGER PRIMARY KEY, service text, date text)`);
db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
} catch(err) {
console.log(err)
}
const db = getDB();
const dbGetAsync = util.promisify(db.get);
exports.get = async function(service) {
let sql = `SELECT Id id,
Service service,
Date date
FROM services
WHERE service = ?`;
const row = await dbGetAsync(sql, [service], (err, row) => {
if (err) {
console.error(err.message);
reject(err)
}
let this_row = {'row': row.id, 'service': row.service};
this_row ? console.log(row.id, row.service, row.date) : console.log(`No service found with the name ${service}`);
resolve(this_row)
});
return row;
}
let row = exports.get('blah')
Run Code Online (Sandbox Code Playgroud)
它说问题出在第31行: const dbGetAsync = util.promisify(db.get);
$ mocha src/tests/testStates.js
C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:1163
else throw err
^
TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type Function. Received type undefined
at Object.promisify (internal/util.js:256:11)
at Object.<anonymous> (C:\Users\Cody\Projects\goggle-indexer\src\state.js:32:25)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\Users\Cody\Projects\goggle-indexer\src\tests\testStates.js:7:15)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:327:14)
at Mocha.run (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:804:10)
at Object.exports.singleRun (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:207:16)
at exports.runMocha (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\lib\cli\run.js:296:3)
at Object.runCommand (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:1087:28)
at Object.parse (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\lib\cli\cli.js:63:6)
at Object.<anonymous> (C:\Users\Cody\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Run Code Online (Sandbox Code Playgroud)
我在使用这个promisify库时遇到问题。任何帮助表示赞赏。
首先不需要return db;在new Promise() 中使用,因为它不期望回调函数有任何返回值。
由于 getDB() 是一个异步函数,它需要与await关键字一起使用才能获取值或在.then.
你getDB()多次打电话对我来说没有意义。
如果不是直接将匿名函数分配给这样的导出对象键exports.get = async function(),然后从导出对象中使用它以在同一文件中使用,最好先阅读,最好定义一个命名的 get 函数,然后使用它以及导出它。
您在new promise()构造函数之外使用了 reject 和 resolve 关键字,这是不可能的。
我已经重写了您的代码,我不确定我是否遗漏了任何内容,但是请看一看,如果您仍然面临任何问题,请告知。
const sqlite3 = require("sqlite3").verbose();
const util = require("util");
async function getDB() {
return new Promise(function(resolve, reject) {
let db = new sqlite3.Database("./project.db", err => {
if (err) {
console.error(err.message);
reject(err);
} else {
console.log("Connected to the project database.");
resolve(db);
}
});
});
}
try {
// run these statements once to set up the db
let db = await getDB();
db.run(
`CREATE TABLE services(id INTEGER PRIMARY KEY, service text, date text)`
);
db.run(
`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`
);
} catch (err) {
console.log(err);
}
const db = await getDB();
const dbGetAsync = util.promisify(db.get);
async function get(service) {
let sql = `SELECT Id id, Service service, Date date FROM services WHERE service = ?`;
try {
const row = await dbGetAsync(sql, [service]);
let this_row = { row: row.id, service: row.service };
this_row
? console.log(row.id, row.service, row.date)
: console.log(`No service found with the name ${service}`);
return row;
} catch (err) {
console.error(err.message);
}
}
let row = await get("blah");
exports.get = get;
Run Code Online (Sandbox Code Playgroud)
getDB 是一个返回 Promise 的异步函数,因此您必须await为 promise 解析或链接 athen以使用其返回值:
// you have to put it inside an async function
const db = await getDB();
const dbGetAsync = util.promisify(db.get);
Run Code Online (Sandbox Code Playgroud)
getDB().then(function(db){
return util.promisify(db.get);
}).then(function(getFunction){
// use get
})
Run Code Online (Sandbox Code Playgroud)