当前位置: 首页 > 知识库问答 >
问题:

TypeError[ERR_INVALID_ARG_TYPE]:原始参数的类型必须是Function。接收类型未定义

韩良策
2023-03-14

在以下代码中,我得到了此错误:

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')

它说问题在第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)

我在使用promisify图书馆时遇到问题。

共有3个答案

仉嘉泽
2023-03-14

getDB是一个返回Promise的异步函数,因此您必须等待promise来解析或链接一个,然后使用其返回的值:

// you have to put it inside an async function
const db = await getDB();
const dbGetAsync = util.promisify(db.get);
getDB().then(function(db){
  return util.promisify(db.get);
}).then(function(getFunction){
  // use get
})
苍元章
2023-03-14

首先不需要使用返回db 在new Promise()中,因为它不希望回调函数返回任何值。

由于getDB()是一个异步函数,因此需要将其与wait关键字一起使用以获取值,或者在的处理函数中可用。然后

您多次调用getDB()对我来说毫无意义。

它是更好地阅读,如果而不是直接分配一个匿名函数导出对象键像这样exports.get=async函数(),然后使用它从导出对象在同一个文件中使用,这将是更好地定义一个名为get的函数,然后使用它以及导出它。

您正在使用拒绝和解析关键字之外的新promise()构造函数,这是不可能的。

我已经重写了你的代码,我不确定我是否遗漏了什么,但是请看一看,如果你仍然面临任何问题,请通知我。

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;

谭景福
2023-03-14

我得到了这个错误,因为我使用的是旧的节点版本(8.17.0),更新节点到一个新的版本(12.14.0)修复了这个错误。

 类似资料: