gpt4 book ai didi

node.js - 类型错误 : Cannot read property 'find' of undefined from sequelize model

转载 作者:太空宇宙 更新时间:2023-11-03 23:28:47 25 4
gpt4 key购买 nike

我正在尝试使用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);
});
}
));

};

最佳答案

在sequelize的import方法完成工作后,您应该获得您的User模型。

所以在你的情况下你应该这样做:

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

这应该有效。

关于node.js - 类型错误 : Cannot read property 'find' of undefined from sequelize model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40707496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com