Javascript Knex要求安装sqlite3,但我';我使用mysql数据库

Javascript Knex要求安装sqlite3,但我';我使用mysql数据库,javascript,node.js,knex.js,node-sqlite3,Javascript,Node.js,Knex.js,Node Sqlite3,我试图对我的本地MySQL数据库进行查询,但当我使用knex时,它告诉我安装sqlite3的驱动程序,我不需要它,因为我的数据库是MySQL 我试图安装sqlite3的驱动器,只是想看看它是否能解决我的问题,但它只是导致出现了与sqlite3相关的其他错误,所以我只是删除了sqlite3驱动程序 我已经有了MySQL的驱动程序。下面是我的代码 为什么会发生这种错误?据我所知,它不应该要求我使用sqlite3驱动程序,而应该要求我在为knex提供配置时使用MySQL ***我已经尝试使用驱动程序m

我试图对我的本地MySQL数据库进行查询,但当我使用knex时,它告诉我安装sqlite3的驱动程序,我不需要它,因为我的数据库是MySQL

我试图安装sqlite3的驱动器,只是想看看它是否能解决我的问题,但它只是导致出现了与sqlite3相关的其他错误,所以我只是删除了sqlite3驱动程序

我已经有了MySQL的驱动程序。下面是我的代码

为什么会发生这种错误?据我所知,它不应该要求我使用sqlite3驱动程序,而应该要求我在为knex提供配置时使用MySQL

***我已经尝试使用驱动程序mysql2,但没有任何改变

const knex = require('knex');
require('dotenv').config();

const listUsers = async (req, res) => {

    console.log(process.env.DATABASE_PASSWORD);
    //res.send(process.env.DATABASE_USER);

    knex({
        client: 'mysql',
        connection: {
            host: process.env.DATABASE_HOST,
            user: process.env.DATABASE_USER,
            password: process.env.DATABASE_PASSWORD,
            database: process.env.DATABASE_NAME
        }
    })

    knex('users')
        .select('*')
        .then(mysqlres => res.send(mysqlres))
        .catch(mysqlerr => res.send(console.log(mysqlerr)))
}

module.exports = {
    listUsers
};

这是我的错误:


Knex: run
$ npm install sqlite3 --save
Cannot find module 'sqlite3'
Require stack:
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\knex.js
- D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js
- D:\coding\Studies\Moblex\backend-mysql\src\routes.js
- D:\coding\Studies\Moblex\backend-mysql\src\server.js
Error: Cannot find module 'sqlite3'
Require stack:
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\knex.js
- D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js
- D:\coding\Studies\Moblex\backend-mysql\src\routes.js
- D:\coding\Studies\Moblex\backend-mysql\src\server.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Client_SQLite3._driver (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js:36:12)    
    at Client_SQLite3.initializeDriver (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:232:26)
    at Client_SQLite3.Client (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:70:10)
    at new Client_SQLite3 (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js:18:10)        
    at new Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:53:28)
    at Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:17:12)
(node:12648) UnhandledPromiseRejectionWarning: Error: Knex: run
$ npm install sqlite3 --save
Cannot find module 'sqlite3'
Require stack:
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\knex.js
- D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js
- D:\coding\Studies\Moblex\backend-mysql\src\routes.js
- D:\coding\Studies\Moblex\backend-mysql\src\server.js
    at Client_SQLite3.initializeDriver (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:236:13)
    at Client_SQLite3.Client (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:70:10)
    at new Client_SQLite3 (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js:18:10)        
    at new Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:53:28)
    at Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:17:12)
    at listUsers (D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js:19:5)
    at Layer.handle [as handle_request] (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\layer.js:95:5)  
    at next (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\layer.js:95:5)  
(node:12648) 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 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12648) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


所以伙计们,我只是想,当我应用配置时,我需要创建一个新的Knex实例,这将起作用!它之前要求使用驱动器sqlite3的原因是因为knex使用sqlite3模型。所以,因为我没有用自己的配置创建Knex的新实例,所以它使用的是默认配置

这是我的解决方案代码

const listUsers = async (req, res) => {


   const knexapp = knex({
        client: 'mysql2',
        connection: {
            database: process.env.DATABASE_NAME,
            user: process.env.DATABASE_USER,
            password: process.env.DATABASE_PASSWORD
        },
        pool: {
            min: 2,
            max: 10
        },
        migrations: {
            tableName: 'knex_migrations'
        }
    })

    knexapp('users').select('*').then(resmsql => res.send(resmsql)).catch(err => res.send(err))