尝试使用Restify API中的Mongoose保存模型时,响应不会返回

我正在使用Restify和Mongoose为NodeJS构build一个API。 在下面的方法find用户并validation他们的密码后,我试图保存一些login信息,然后将响应发回给用户。 问题是响应永远不会返回。 如果我在保存调用之外和之后放置响应,数据永远不会持久保存到MongoDB。 难道我做错了什么? 在过去的两天里,我一直在为此工作提供帮助。

login: function(req, res, next) { // Get the needed parameters var email = req.params.email; var password = req.params.password; // If the params contain an email and password if (email && password) { // Find the user findUserByEmail(email, function(err, user) { if (err) { res.send(new restify.InternalError()); return next(); } // If we found a user if (user) { // Verify the password user.verifyPassword(password, function(err, isMatch) { if (err) { res.send(new restify.InternalError()); return next(); } // If it is a match if (isMatch) { // Update the login info for the user user.loginCount++; user.lastLoginAt = user.currentLoginAt; user.currentLoginAt = moment.utc(); user.lastLoginIP = user.currentLoginIP; user.currentLoginIP = req.connection.remoteAddress; user.save(function (err) { if (err) { res.send(new restify.InternalError()); return next(); } // NEVER RETURNS!!!! // Send back the user res.send(200, user); return next(); }); } else { res.send(new restify.InvalidCredentialsError("Email and/or password are incorrect.")); return next(); } }); } else { res.send(new restify.InvalidCredentialsError("Email and/or password are incorrect.")); return next(); } }); } else { res.send(new restify.MissingParameterError()); return next(); } }, 

这个问题的一个原因可能是,如果你有一个pre save hook默默的错误。

如果你发现你的模型是一个.pre('save' () => {...})函数,那么请在调用save之后再次检查这个方法,并且返回时没有错误。

有关mongoose中间件的文档可以在这里find