my express server is giving me the error: C:.........\server\src\models\index.js:8 config.db.database, ^
TypeError: Cannot read properties of undefined (reading 'database') at Object. (C:\Users\1\Documents\GitHub\GFS\server\src\models\index.js:8:15) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (C:\Users\1\Documents\GitHub\GFS\server\src\app.js:5:22) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
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.user,
config.db.password,
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.export = db
heres the config file
module.export = {
port: process.env.PORT || 8081,
db: {
database: process.env.DB_NAME || 'tabtracker',
user: process.env.DB_USER || 'tabtracker',
password: process.env.PASS || 'tabtracker',
options: {
dialect: process.env.DIALECT || 'sqlite',
host: process.env.HOST || 'localhost',
storage: './tabtracker.sqlite'
}
}
}
CodePudding user response:
Seems pretty clear from the error message that this is an issue with the config variable. config.db is being read as undefined.
First thing I would do is confirm that the path on the require "../config/config" is correct. If it is, then this is almost certainly some kind of syntax issue. I've never used the syntax "module.export", always been "module.exports". Seems unlikely that's the issue, and I know that there are multiple ways to accomplish exporting.
I'd be curious to see if you did a console.log(config) on line 6 what would print. Seems like you could zero in on the issue quite a bit by seeing if that is an incomplete object or if it's undefined.