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

前端 未结 4 1901
礼貌的吻别
礼貌的吻别 2021-01-03 07:16

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

All functions (sequelize) are not working. All errors: Cannot rea

4条回答
  •  天涯浪人
    2021-01-03 07:31

    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. :)

提交回复
热议问题