TypeError:无法读取未定义的属性“数据库”

时间:2019-12-07 15:14:32

标签: javascript node.js sequelize.js

我有两个文件-config.js和index.js 我写的代码如下-

config.js-

module.exports={
    port:process.env.PORT || 8081,
    db:{
        database:process.env.DB_NAME|| 'tabtracker',
        user:process.env.DB_USER || 'tabtracker',
        password:process.env.DB_PASS|| 'tabtracker',
        options:{
            dialect:process.env.DIALECT ||'sqlite',
            host:process.env.HOST ||'localhost', 
            storage:'./tabtracker.sqlite'

        }
    }
}

index.js-

const fs=require('fs')
const path=require('path')
const Sequelize=require('sequelize')
const config=require('../config/config')
const db={}


const sequelize=new Sequelize(

    config.db.database,
    config.db.password,
    config.db.user,
    config.db.options
)


fs.readdirSync(__dirname)
 .filter((file)=>{
     file!=='index.js'
 }
 )
.forEach(file=>{
    const model=sequelize.import(path.join(__dirname,file));
    db[model.name]=model
})

db.sequelize=sequelize
db.Sequelize=Sequelize
module.exports=db

为什么会引发错误?我已经在index.js中正确导入了config.js。我也正确声明了配置文件。

1 个答案:

答案 0 :(得分:1)

const config=('../config/config')-此处没有require关键字。因此config不是您的配置,只是右侧的字符串。因此,访问undefined的属性,即config.db