Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS unable to import Sequelize.js model (ES6)

I'm running NodeJS with Express and Seqeulize and I have a file controllers/rooms.js importing Room from 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))
}

Bellow is models/room.js (There is also index.js file generate by sequelize-cli in the same directory)

'use strict'

export default (sequelize, DataTypes) => {

    const Room = sequelize.define('Room', {
        name: DataTypes.STRING
    })

    return Room
}

And I have a route app.get('/rooms', list), but when I access this route I get this error:

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)

I'm sure I have my imports/exports messed up but I don't know how.

models/index.js file bellow

'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

It works when I call it like this: return Room.Room.findAll()... Therefore i can rename the import to this: import models from '../models' and call it this way: return models.Room.findAll()...

However why can't I just call it return Room.findAll()..., how should the import be formulated ??

like image 337
Matis Avatar asked Oct 20 '25 20:10

Matis


1 Answers

Assuming you have babel setup for using ES6 imports you can try this approach to export a Sequelize model in ES6.

// db config file

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
  }
);

// Model

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;
like image 89
Fateh Farooqui Avatar answered Oct 23 '25 10:10

Fateh Farooqui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!