【发布时间】:2014-08-12 23:21:19
【问题描述】:
在尝试调试自己并搜索谷歌后,我的猫鼬 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);
});
}
});
});
}));
-----------------创建的数据客户端(用户控制器)
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 添加了一个错误事件侦听器,但没有任何反应。
我的快递服务器也只在连接打开后运行,因此排除了这种情况。
您能提供的任何帮助将不胜感激!
-斯蒂芬
【问题讨论】:
-
你在某个地方给
mongoose.connect打电话吗? -
在启动服务器时是。 “我的快递服务器也只在连接打开后才运行,因此排除了这种情况。”
-
来吧!我知道你能做到!
-
你能用一个最小的例子重现这个问题吗?你在这里有很多事情要做。
-
这是重现此“错误”的代码和路径。带有护照模块的 Express 应用程序。然后把它放进去。任何少的东西都违背了我在这里发帖的目的。我可以在自运行脚本中完全访问和查询 mongo。正是这条通过我的应用程序的路线停滞了
标签: node.js mongodb express mongoose