NodeJS无法导入Sequelize.js模型(ES6)

时间:2018-10-25 11:08:18

标签: node.js express ecmascript-6 sequelize.js

我正在使用Express和Seqeulize运行NodeJS,并且有一个文件controllers/rooms.jsmodels/room.js导入Room。

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(在同一目录中还有sequelize-cli生成的index.js文件)

'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()...,导入应该如何制定?

3 个答案:

答案 0 :(得分:4)

假设您已经设置了使用ES6导入的babel设置,则可以尝试这种方法在ES6中导出Sequelize模型。

// db配置文件

import Sequelize from 'sequelize';

export const sequelize = new Sequelize(
  config.database.name,
  config.database.user,
  config.database.password,
  {
    host: config.database.host,
    dialect: config.database.dialect,
    pool: config.database.pool,
    operatorsAliases: false
  }
);

//型号

import Sequelize from 'sequelize';
import { sequelize } from '../database/db';

const User = sequelize.define(
  'table_name',
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    email: {
      type: Sequelize.STRING,
      allowNull: false
    },
    password: {
      type: Sequelize.STRING,
      allowNull: false
    }
  },
  { freezeTableName: true }
);

export default User;

答案 1 :(得分:0)

上次使用Sequelize时,它与ES6功能的配合不佳。我的猜测是,models/room.js由于export default无法正确导出模型。您可以尝试将该行更改为旧式module.exports;

export default (sequelize, DataTypes) => {
.....
To
.....
module.exports = (sequelize, DataTypes) => {

看看是否能解决导入问题。

在控制器中导入时,您可以执行此操作;

const Room = require('../models').Room;

这是导入的旧方法,现在您的代码应该可以工作了。 :)

如果要尝试使用ES6,可以执行类似的操作;

import {Room} from '../models'

我不确定此ES6导入是否在这里有效!

答案 2 :(得分:0)

我在尝试设置现有代码时遇到了上述问题。通过将我的节点版本从最新版本降级到 10.18.0,问题得到解决。

似乎代码最初是使用节点版本 10 开发的。