Skip to content
Advertisement

AuthenticationController always throws error, TypeError: Cannot read property ‘create’ of undefined

I have an issue with the Authenticati onController used with sequelize and sqlite When I test the POST request using postman, it always gives status 400 with the response { error: ‘Something is wrong’ } enter image description here This is the log

::1 – – [21/Jul/2020:15:40:33 +0000] “POST /register HTTP/1.1” 400 30 “-” “PostmanRuntime/7.26.1”

enter image description here Here is the code of AuthenticationController

const {User} = require('../models')
module.exports = {
    async register(req, res){
        try {   
            const user = await User.create(req.body)
            res.send(user.toJSON())
        } catch (error) {
            console.log(error)
            res.status(400).send({
                error: 'Something is wrong'
            })
        }
    }
}

User model code

module.exports = (sequelize, DataTypes) =>
sequelize.define('User', {
    email: {
        type: DataTypes.STRING,
        unique: true
    },
    password: DataTypes.STRING
})

models index code

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 = require(path.join(__dirname, file))(
          sequelize,
          Sequelize.DataTypes
        );
        // const model = sequelize.import(path.join(__dirname,file))
        db[model.name] = model
    })
    db.sequelize = sequelize
    db.Sequelize = Sequelize

module.exports = db

routes code

const AuthenticationController = require('./controllers/AuthenticationController');

module.exports = (app) => {
    app.post('/register',
     AuthenticationController.register)
}

Earlier, it was throwing an error of “TypeError: Cannot read property ‘create’ of undefined” But when I restarted the server, that was fixed. But I have no clues as to why the try block fails.

Anyone could throw some light on this? Thank you

Advertisement

Answer

Here is the revised code for /models/index.js

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";
    //   })
  .filter(
    (file) =>
      file.indexOf(".") !== 0 && file !== "index.js" && file.slice(-3) === ".js"
  )
  .forEach((file) => {
    const model = require(path.join(__dirname, file))(
      sequelize,
      Sequelize.DataTypes
    );
    // const model = sequelize.import(path.join(__dirname,file))
    db[model.name] = model;
  });
    db.sequelize = sequelize
    db.Sequelize = Sequelize

module.exports = db

The filter block was updated as shown in the revised, with the help of the link https://www.codota.com/code/javascript/functions/sequelize/Sequelize/import The 2 things that were changed are

  1. Replaced the ‘sequelize.import’ with ‘require’
  2. Updated the ‘filter’ block as shown.

Here is the result: enter image description here

Thanks to crumkev for the insight which led me find the answer.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement