NodeJS 什么是“配置未定义”

bis0qfac  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(142)

我是为了mariadb的连接。但是我面对这个错误。这是什么?我是Node初学者。我想得到专业咨询。node.js作为初学者是错误的。我等着纠正你的建议

var sequelize = new _sequelize2.default(config.database, config.username, config.password, 
config);
                                        ^

ReferenceError: config is not defined
    at Object.<anonymous> (C:/Users/mpkw1/src/models/index.js:9:33)
    at Module._compile (module.js:569:30)
    at loader (C:\Users\mpkw1\AppData\Roaming\nvm\v8.1.2\node_modules\babel-cli\node_modules\babel-register\lib\node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (C:\Users\mpkw1\AppData\Roaming\nvm\v8.1.2\node_modules\babel-cli\node_modules\babel-register\lib\node.js:154:7)       
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:/Users/mpkw1/src/index.js:5:1)
    at Module._compile (module.js:569:30)
    at loader (C:\Users\mpkw1\AppData\Roaming\nvm\v8.1.2\node_modules\babel-cli\node_modules\babel-register\lib\node.js:144:5)

这是midels文件夹中的User.js

module.exports = (sequelize, DataTypes) => {
    return sequelize.define('user', {
        user_id : { ... },
        id : { ... },
        password : { ... },
        nickname : { ... }
    });
};

这是配置文件夹中的sequelize.js

{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mariadb",
    "operatorsAliases": false
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mariadb",
    "operatorsAliases": false
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mariadb",
    "operatorsAliases": false
  }
}

在src/index.js

const Koa = require('koa');
const app = new Koa();

//const sequelize = require('./models');
import { sequelize } from './models';
sequelize.sync().then(() => {
    console.log('✓ DB connection success.');
})
.catch(err => {
    console.error(err);
    console.log('✗ DB connection error. Please make sure DB is running.');
    process.exit();
});

app.listen(4000, () => {
    console.log('server is listening to port 4000');
});

几乎模块是新版本。

lkaoscv7

lkaoscv71#

看起来你没有在你的index.js文件中定义配置。尝试在index.js文件的顶部添加如下内容:要么

const config = require('config');

或者,如果你使用TypeScript:

import config from 'config';

根据NODE_ENV环境变量的值加载配置文件。如果您没有设置此变量,它将使用config目录中的default.json文件。

相关问题