gpt4 book ai didi

node.js - Mongoose findOne 未完成

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

在尝试调试自己并搜索谷歌后,我的 Mongoose 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 添加了一个错误事件监听器,但没有任何结果。

我的 Express 服务器也仅在连接打开后运行,因此排除了这种情况。

如果您能提供任何帮助,我们将不胜感激!

-斯蒂芬

最佳答案

我也遇到了同样的问题。对我来说问题是我正在使用 mongoose.createConnection

let connection = mongoose.createConnection(url, ...)

但随后尝试使用 Mongoose 来创建模型

let Tank = mongoose.model('Tank', yourSchema);

当您使用 createConnection 时,您需要使用该连接 - https://mongoosejs.com/docs/models.html#constructing-documents .

let Tank = connection.model('Tank', yourSchema);

关于node.js - Mongoose findOne 未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24357112/

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