我正在尝试使用sequelize在我的数据库中查找记录,但我得到了这个:
.find({ where: { name: email } })
TypeError: Cannot read property 'find' of undefined
这是我的 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);
});
这是我的 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;
};
这就是我在文件中导入用户模型的方式:
var User = require('../models/user').User;
编辑
我正在尝试使用用户通过本地护照创建注册。我在 config/passport.js 中调用 User
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);
});
}
));
};
我是一名优秀的程序员,十分优秀!