Я использую NodeJS с Express и Seqeulize, и у меня есть файл controllers/rooms.js
, импортирующий Room из models/room.js
.
import Room from '../models'
export function list(req, res) {
return Room
.findAll()
.then((rooms) => res.status(200).send(rooms))
.catch((error) => res.status(400).send(error))
}
Сильфон * models/room.js
(Существует также index.js
генерация файлапо sequelize-cli в том же каталоге)
'use strict'
export default (sequelize, DataTypes) => {
const Room = sequelize.define('Room', {
name: DataTypes.STRING
})
return Room
}
И у меня есть маршрут app.get('/rooms', list)
, но когда я получаю доступ к этому маршруту, я получаю эту ошибку:
TypeError: _models2.default.findAll is not a function
at list (/Users/matis/Documents/apps/node-docker-test/app/database/controllers/rooms.js:21:10)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at /Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:275:10)
at expressInit (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/middleware/init.js:40:5)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:317:13)
at /Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:275:10)
at query (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/middleware/query.js:45:5)
at Layer.handle [as handle_request] (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:317:13)
at /Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/router/index.js:174:3)
at Function.handle (/Users/matis/Documents/apps/node-docker-test/node_modules/express/lib/application.js:174:10)
ЯЯ уверен, что мой импорт / экспорт испорчен, но я не знаю как.
models/index.js
файл ниже
'use strict'
import { readdirSync } from 'fs'
import { basename as _basename, join } from 'path'
import Sequelize from 'sequelize'
const basename = _basename(__filename)
const env = process.env.NODE_ENV || 'development'
const config = require(__dirname + '/../config/config.json')[env]
const db = {}
let sequelize
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config)
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config)
}
readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
})
.forEach(file => {
const model = sequelize['import'](join(__dirname, file))
db[model.name] = model
})
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db)
}
})
db.sequelize = sequelize
db.Sequelize = Sequelize
export default db
Это работает, когда я называю это так: return Room.Room.findAll()...
Поэтому я могу переименовать импорт в это: import models from '../models'
и назвать его так: return models.Room.findAll()...
Однако, почему я не могу просто назвать его return Room.findAll()...
, каким должен быть импорт?сформулировано ??