尝试调试自己并搜索谷歌后,我的猫鼬findOne没有完成!它到达线路并且没有结束,直到大约2分钟后超时。这是代码的顺序,它被称为
----------------路由
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/', // redirect back to the home page if there is an error
failureFlash : true // allow flash messages
}));
------------护照处理
passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function (req, email, password, done) {
// asynchronous
process.nextTick(function () {
// Whether we're signing up or connecting an account, we'll need
// to know if the email address is in use.
UserController.findUserByEmail(email, function (existingUser) {
console.log(existingUser);
// check to see if there's already a user with that email
if (existingUser)
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
// If we're logged in, we're connecting a new local account.
if (req.user) {
var user = req.user;
user.email = email;
user.password = encrypt.encrypt(password);
user.save(function (err) {
if (err)
throw err;
return done(null, user);
});
}
// We're not logged in, so we're creating a brand new user.
else {
console.log("there");
// create the user
User.createUser(req.body.username, email, req.body.username, password, "0", 1, function (result) {
return done(null, result);
});
}
});
});
}));
------------------创建数据客户端(UserController)
exports.findUserByEmail = function(email, callback){
User.findOne({email : email}, function(err, result){
if(err){
throw err;
}else{
callback(result);
}
});
};
------------------用户架构
//user Schema
var userSchema = mongoose.Schema({
name : String,
email : String,
userName : String,
password : String,
IP : String,
//type of user. It can be a user admin 0, user student 1, user presentor (professor) 2, (user guest 3) ?.
type : Number
});
//instance of my schema
var User = mongoose.model('User', userSchema);
代码在User.findOne()之前运行;然后挂起添加一些调试语句会显示调用该方法的所有内容,但它永远不会进入回调。它只是在等待。
其他架构的其他查询工作正常,并且返回非常快。
我为mongoose添加了一个错误事件监听器,但没有出现。
我的快递服务器也只在连接打开后运行,以便规则出来。
非常感谢您提供的任何帮助!
-Stephen
答案 0 :(得分:2)
好的,首先,我认为你过度复杂了。首先来看看mongoose静态:http://mongoosejs.com/docs/guide.html#statics
现在,为什么需要单独的UserController? (并且因为这不是MVC控制器,您的命名约定令人困惑 - 如果您打算将User和UserController分开,请考虑重命名)。考虑一下,为什么你需要这种方法呢?我的意思是,为什么不直接从中间件函数调用User.findOne({email:"某些电子邮件地址"})?
无论你选择哪种风格......
我认为您的问题是在尝试使用之前,您没有在UserController中获得对User模型的引用。
在UserController中调用User.findOne之前添加此行:
var User = mongoose.model('User');
看看是否有效。 (在尝试使用之前,请确保您的型号是必需的。)
答案 1 :(得分:1)
我遇到了同样的问题。对我来说,问题是我在使用mongoose.createConnection
let connection = mongoose.createConnection(url, ...)
,但随后尝试使用猫鼬创建模型
let Tank = mongoose.model('Tank', yourSchema);
使用createConnection时,需要使用该连接-https://mongoosejs.com/docs/models.html#constructing-documents。
let Tank = connection.model('Tank', yourSchema);
答案 2 :(得分:0)
我已经解决了这个问题:
Controller File是父目录中另一个节点项目的一部分。我假设然后节点没有默认读取当前节点进程加载的模块,但是试图读取当时没有运行的其他节点项目。只需将文件移动到同一节点项目文件夹即可解决问题。