我試圖用sequelize找到我的數據庫中的記錄,但我發現了這一點:類型錯誤:無法讀取sequelize模型屬性「發現」的不確定
.find({ where: { name: email } })
TypeError: Cannot read property 'find' of undefined
這是我的模型/ 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);
});
這是我的模型/ 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;
編輯
我試圖使用用戶創建與護照本地註冊。 我在配置主叫用戶/ 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);
});
}
));
};
你可以在'.find'附近看到更多的代碼,比如你使用什麼對象,以及如何獲得這個對象? – drinchev
@drinchev我編輯了這個問題。你可以檢查一下嗎? –