Filipe Ferminiano Filipe Ferminiano - 17 days ago 9
Node.js Question

TypeError: Cannot read property 'find' of undefined from sequelize model

I'm trying to use sequelize to find a record in my database, but I'm getting this:

.find({ where: { name: email } })
TypeError: Cannot read property 'find' of undefined


This is my models/index.js

var Sequelize = require('sequelize')
, sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
port: 3306, // or 5432 (for postgres)
});

// load models
var models = [
'user',
];

models.forEach(function(model) {
module.exports[model] = sequelize.import(__dirname + '/' + model);
});


And this is my models/user.js

var Sequelize = require("sequelize");

module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
email: DataTypes.STRING,
password: DataTypes.STRING,
token: DataTypes.STRING
}, {
tableName: 'users'
}

);

return User;
};


This is how I'm importing user model in my files:

var User = require('../models/user').User;


EDIT

I'm trying to use User to create signup with passport local.
I'm calling User in config/passport.js

var LocalStrategy = require('passport-local').Strategy;
var SlackStrategy = require('passport-slack').Strategy;
var User = require('../models').User;
var mysql = require('mysql');
var connection = mysql.createConnection({
host : process.env.MYSQL_HOST,
user : process.env.MYSQL_USER,
password : process.env.MYSQL_PASSWORD,
database : process.env.MYSQL_DB
});

module.exports = function(passport) {
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
console.log("abc serializeUser");
console.log(user);
done(null, user.id);
});

passport.deserializeUser(function(id, done) {
console.log("abc deserializeUser");
User.findById(id).then(function(user){
done(null, user);
}).catch(function(e){
done(e, false);
});
});

passport.use('local-signup', new LocalStrategy(
{
// by default, local strategy uses username and password, we will override with email
usernameField : 'name',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},

function(req, email, password, done)
{

process.nextTick(function() {

User
.find({ where: { name: email } })
.then(function(err, user) {
if (!user) {
console.log('No user with the username "john-doe" has been found.');
} else {
console.log('Hello ' + user.name + '!');
console.log('All attributes of john:', user.get());
}
});


}
));


passport.use('slack', new SlackStrategy({
clientID: process.env.SLACK_ID,
clientSecret: process.env.SLACK_SECRET,
scope: "users:write"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ SlackId: profile.id }, function (err, user) {
return done(err, user);
});
}
));

};

Answer

You should get your User model after the import method of sequelize does it's job.

So in your case you should do :

var User = require('../models/index').User;

This should work.