问题
I have two files-config.js and index.js The code I have written is as follows-
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
Why it is throwing error? I have imported the config.js properly in index.js. I have also declared config file properly.
回答1:
const config=('../config/config')
- there is no require
keyword here. So the config
is not your config, just a string on the right. Hence accessing property of undefined
, which is config.db
.
来源:https://stackoverflow.com/questions/59227392/typeerror-cannot-read-property-database-of-undefined