0

I have followed the Sequelize documentation, the database has been created successfully, but I can't access my model methods. When I log the Unit model, it only returning undefined. (Cannot read property 'findAll' of undefined).

Am I doing something wrong?

The controller and models look like below:

controller/unit.js

const Unit = require('../models').Unit;
const errorHandler = require('../helpers/error_handler');

exports.findAll = async (req, res) => {
    try {
        const unit = Unit.findAll();
        return res.status(200).send({
            status: 200,
            response: unit
        });
    } catch (error) {
        req.error = error.message;
        return errorHandler.http500(req, res);
    }
};

models/index.js

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.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);
}

fs
    .readdirSync(__dirname)
    .filter((file) => {
        return file.indexOf('.') !== 0 && file !== basename && file.slice(3) === '.js';
    })
    .forEach((file) => {
        const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
        // const model = require(path.join(__dirname, file))(sequelize, Sequelize);

        db[model.name] = model;
    });

Object.keys(db).forEach((modelName) => {
    if (db[modelName].associate) {
        db[modelName].associate(db);
    }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

models/unit.js

'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
    class Unit extends Model {
        /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
        static associate(models) {
            // define association here
        }
    }
    Unit.init(
        {
            id_eksternal: DataTypes.STRING,
            nama: DataTypes.STRING
        },
        {
            sequelize,
            modelName: 'Unit'
        }
    );
    return Unit;
};

fxbayuanggara
  • 316
  • 3
  • 16

2 Answers2

0

I cannot comment so I post my answer here. I think you need to console.log the model.name in the line db[model.name] = model; to see what is put in db, and also other console.log after require('../models') inside your controller to see how the db looks like. Maybe it is just about naming of your model

Andy Nguyen
  • 319
  • 2
  • 4
0

I've made a mistake.

The line in models/index.js:

return file.indexOf('.') !== 0 && file !== basename && file.slice(3) === '.js';

should be

return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';

then in the association settings, I set models.TableName instead of models.ModelName, so my Controller can't find the model file name.

fxbayuanggara
  • 316
  • 3
  • 16