类型错误:无法读取未定义的属性“数据库" [英] TypeError: Cannot read property 'database' of undefined
本文介绍了类型错误:无法读取未定义的属性“数据库"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个文件-config.js 和 index.js我写的代码如下-
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
为什么会抛出错误?我已经在 index.js 中正确导入了 config.js.我也正确地声明了配置文件.
Why it is throwing error? I have imported the config.js properly in index.js. I have also declared config file properly.
推荐答案
const config=('../config/config')
- 没有 require
关键字这里.所以 config
不是你的配置,只是右边的一个字符串.因此访问undefined
的属性,即config.db
.
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
.
这篇关于类型错误:无法读取未定义的属性“数据库"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文