TypeError [ERR_INVALID_ARG_TYPE]: The quot;originalquot; argument must be of type Function. Received type undefined(TypeError[ERR_INVALID_ARG_TYPE]:quot;原始quot;参数必须是函数类型。接收的类型未定义)
问题描述
在以下代码中,我收到以下错误:
TypeError[ERR_INVALID_ARG_TYPE]:";Original";参数必须是函数类型。接收的类型未定义
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')
它说问题在第31行:const dbGetAsync = util.promisify(db.get);
$ mocha src/tests/testStates.js
C:UsersCodyAppDataRoaming
pm
ode_modulesmocha
ode_modulesyargsyargs.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:UsersCodyProjectsgoggle-indexersrcstate.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:UsersCodyProjectsgoggle-indexersrc ests estStates.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:UsersCodyAppDataRoaming
pm
ode_modulesmochalibmocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (C:UsersCodyAppDataRoaming
pm
ode_modulesmochalibmocha.js:327:14)
at Mocha.run (C:UsersCodyAppDataRoaming
pm
ode_modulesmochalibmocha.js:804:10)
at Object.exports.singleRun (C:UsersCodyAppDataRoaming
pm
ode_modulesmochalibcli
un-helpers.js:207:16)
at exports.runMocha (C:UsersCodyAppDataRoaming
pm
ode_modulesmochalibcli
un-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (C:UsersCodyAppDataRoaming
pm
ode_modulesmochalibcli
un.js:296:3)
at Object.runCommand (C:UsersCodyAppDataRoaming
pm
ode_modulesmocha
ode_modulesyargslibcommand.js:242:26)
at Object.parseArgs [as _parseArgs] (C:UsersCodyAppDataRoaming
pm
ode_modulesmocha
ode_modulesyargsyargs.js:1087:28)
at Object.parse (C:UsersCodyAppDataRoaming
pm
ode_modulesmocha
ode_modulesyargsyargs.js:566:25)
at Object.exports.main (C:UsersCodyAppDataRoaming
pm
ode_modulesmochalibclicli.js:63:6)
at Object.<anonymous> (C:UsersCodyAppDataRoaming
pm
ode_modulesmochain\_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)
我在使用此Promisify库时遇到问题。
推荐答案
首先不需要在new Promise()内部使用return db;
,因为它不需要来自回调函数的任何返回值。
由于getDB()是异步函数,需要与await
关键字配合使用才能获取值,或者在.then
的处理函数中可用。
您多次调用getDB()
对我来说没有意义。
与其像exports.get = async function()
这样直接将匿名函数分配给Exports对象键,然后从Exports对象中使用它在同一文件中使用,不如定义一个命名的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;
这篇关于TypeError[ERR_INVALID_ARG_TYPE]:";原始";参数必须是函数类型。接收的类型未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError[ERR_INVALID_ARG_TYPE]:";原始";参数必须是函数类型。接收的类型未定义
基础教程推荐
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在for循环中使用setTimeout 2022-01-01