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

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]:"原始"参数必须是函数类型。接收的类型未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,我收到以下错误:

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函数,然后在导出的同时使用它。

更好的做法是将其直接分配给Exports对象键,然后从Exports对象中使用它以在同一文件中使用。

您正在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]:&quot;原始&quot;参数必须是函数类型。接收的类型未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)