我目前正在我的Node js应用程序中从mongodb迁移到MySQL。我使用sequelize作为ORM,但是我在迁移一些passportjs代码时遇到了一些麻烦。
我有以下模式。
user.js的:
"use strict";
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("users", {
username: DataTypes.STRING,
localemail: DataTypes.STRING,
localpassword: DataTypes.STRING,
facebookid: DataTypes.STRING,
facebooktoken: DataTypes.STRING,
facebookemail: DataTypes.STRING,
facebookname: DataTypes.STRING,
twitterid: DataTypes.STRING,
twittertoken: DataTypes.STRING,
twitterdisplayname: DataTypes.STRING,
twitterusername: DataTypes.STRING,
googleid: DataTypes.STRING,
googletoken: DataTypes.STRING,
googleemail: DataTypes.STRING,
googlename: DataTypes.STRING
});
return User;
};
我的passportjs文件中有以下功能:
...
// load all the things we need
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
// load up the user model
var User = require('../models/user');
...
passport.use('local-login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
if (email)
email = email.toLowerCase();
// asynchronous
process.nextTick(function() {
User.findOne({
where: {
localemail: email
}
}).then(function(user) {
// if there are any errors, return the error
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.'));
if (!validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
// all is well, return user
else
return done(null, user);
});
});
}));
...
应用程序退出时出现以下错误:
passport.js:100
User.findOne({
^
TypeError: User.findOne is not a function
我在github上查看了这段代码的灵感:
https://github.com/sequelize/express-example
任何关于我忽略的想法?
更新:
所以在我的passport.js文件中我已经这样做了:
var models = require('../models');
console.log("models.User:" +models.User);
输出:
npm start app.js
> myapp0.0.0 start /home/mathias/nodejs/myapp
> DEBUG=express-sequelize
node ./bin/www "app.js"
models.User:undefined
express-sequelize
Express server listening on port 3000 +0ms express-sequelize
Listening on port 3000 +7ms –
这在控制台中提供了[object:object]:
var models = require('../models');
console.log("models:" +models);
这也给出了一个未定义的:
var models = require('../models').User;
console.log("models:" +models);
使用config.json文件进行更新:
{
"development": {
"username": "root",
"password": "password",
"database": "myapp",
"host": "0.0.0.0",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": "password",
"database": "myapp",
"host": "0.0.0.0",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": "password",
"database": "myapp",
"host": "0.0.0.0",
"dialect": "mysql"
}
}
答案 0 :(得分:3)
问题在于您需要该模块。替换:
var User = require('../models/user');
有了这个:
var User = require('../models').User;
这是因为模型动态导出的方式。你可以看一下你的文件models / index.js。在那里,您将找到如何在单个对象中导出每个模型。因此,您基本上总是需要models / index.js,并指定您要访问的密钥,在本例中为“User”
答案 1 :(得分:0)
文件user.js
导出一个返回模型的函数:
module.exports = function(sequelize, DataTypes) {
...
return User;
}
因此,为了获得该模型引用,您需要使用以下内容:
const Sequelize = require('sequelize');
const DataTypes = sequelize.DataTypes;
let sequelize = new Sequelize(...);
const User = require('../models/user')(sequelize, DataTypes);
但是,我不明白为什么你想要传递 sequelize
和DataTypes
,因为你可以轻松地将它们导入模型文件中(反过来,它可以直接导出模型):
// models/user.js
const sequelize = require('sequelize');
const DataTypes = sequelize.DataTypes;
module.exports = sequelize.define("users", { ... });
使用:
const User = require('../models/user');
答案 2 :(得分:0)
使用对象解构效果更好
var { User } = require('../models');