TypeError: Cannot read property 'findAll' of undefined (expressjs)
Asked Answered
B

8

8

TypeError: Cannot read property 'findAll' of undefined (expressjs).

All functions (sequelize) are not working. All errors: Cannot read property 'sequelize method' ...

module.exports = function (sequelize, DataTypes) {
var User = sequelize.define('user', {
    email: {type: DataTypes.STRING(32), unique: true, allowNull: false},
});

return User;
};

Controller:

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

exports.index = function (request, response, next) {
    models.User.findAll({attributes: ['id', 'username']});
};
Boothman answered 14/1, 2016 at 17:28 Comment(4)
please write content of your ./../models/index.js or if you init models in non standard way, please write it instead.Porous
@KrzysztofSztompka, pastebin.com/Ev5Mp2ADBoothman
thanks, please write how you run dbPorous
@KrzysztofSztompka, app.js pastebin.com/xwNV1HmuBoothman
B
12

I had the same issue, and the changes below worked for me. This might be useful for future users -

when you use the sequelize model you need to use the defined model class name, which is "user" instead of "User" in line findAll:

models  = require('./../models');
exports.index = function (request, response, next) {
    models.user.findAll({attributes: ['id', 'username']});
};

The "User" is a variable to which the sequelize definition is assigned and its not recognized outside that model definition. The "user" is the table name that is getting created as well as the sequelize model class name, and this needs to be used in any type of db query.

Bresee answered 4/10, 2017 at 15:16 Comment(0)
P
4

You have created two instances of sequelize. One in models/index.js in line 12/14 and second instance in server script in line 19. And you start second instance, but in model you tried to use first instance.

Your model/index.js file is ok. In your server file add

var database = require('path/to/models');

change your db start to:

database.sequelize .authenticate() .then(function(err) { console.log('Connection has been established successfully.'); }, function (err) { console.log('Unable to connect to the database:', err); });

And you have to pass 'database' object to your controllers instead of models = require('./../models'); and from your controllers you have access to your model : database.User

Porous answered 14/1, 2016 at 19:35 Comment(4)
Hm, ReferenceError: database is not defined at exports.index (c:\webserver\data\htdocs\newcloudy\controllers\user.js:5:5) pastebin.com/tS89jbBpBoothman
how you pass database object from server script to controller? It looks like there is some problem with itPorous
var database = require('./../app.js'); and TypeError: Cannot read property 'findAll' of undefined.Boothman
No, you can't do it like that. You run your controllers in server script. You have to pass database instance to your controllers in server script. Please check this https://mcmap.net/q/1323768/-how-do-i-modularise-a-node-and-express-application and 'second edit'.Porous
L
1

Your model/index.js looks fine. In your controller try findAll() method inside sequelize.sync().then(function () {

Here is my approach to the problem

nb: instead of models/index.js i have a similar db.js file inside config folder which having the dbconnection scripts and sequelize object.

My userController.js will look like (working code) :

var db = require('./../config/db'),
seq = db.seq,
Sequelize = db.Sequelize;

module.exports = function(app) {
    app.get('/getUsers',function(req,res){
        var Users = require('../models/UserModel')(app); //since i am binding to a single object - app
        seq.sync().then(function () {
            Users.findAll({
               attributes: ['usr_code', 'usr_name']
           }).then(function (users) {
                    users.forEach(function(user,index,arr){
                        console.log(user.usr_code);
                    });
                });
        });
    });

}

Hope this helps you. :)

Lipfert answered 7/2, 2017 at 18:23 Comment(0)
I
1

This error is because you intend to use Sequelise in the controller before fully connecting it to the database

try use .then or async await

Incomplete answered 29/7, 2022 at 19:2 Comment(0)
S
0

I also had the same issue, you need to check your table name that's it.

Sesquicarbonate answered 2/1, 2021 at 11:17 Comment(0)
H
0

you may have this issue when relationships or associations are not defined with db connection.

Halbert answered 1/6, 2021 at 7:30 Comment(0)
D
0

I just had the same problem, a few minutes ago.

This is how I imported the models:

const {product: Product, cartItem: CartItem} = require('../models');
Dispread answered 30/9, 2022 at 2:20 Comment(0)
W
0

added to models/index.js

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

// db.your_model
db.Note = require('./note')(sequelize, Sequelize);

module.exports = db;```
Woodsia answered 12/8 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.