node.js - 尝试在 Restify API 中使用 Mongoose 保存模型时不会返回响应

标签 node.js mongodb mongoose restify

我正在使用 Restify 和 Mongoose 为 NodeJS 构建 API。在找到用户并验证其密码后的下面方法中,我试图在将响应发送回用户之前保存一些登录信息。问题是响应永远不会返回。如果我将响应放在保存调用之外和之后,数据永远不会持久保存到 MongoDB。难道我做错了什么?过去 2 天我一直在做这方面的工作,所以帮助会很大。

    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 中间件的文档 here

关于node.js - 尝试在 Restify API 中使用 Mongoose 保存模型时不会返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12974230/

相关文章:

javascript - JSPM - jspm 安装给出错误 "Registry not found"

database - 仅当字段不为空时才将其设置为索引

node.js - Mongoose 更新数组对象中的字段

javascript - Mongoose 在插入文档时调用字段的函数

javascript - 如何在 Express 中修复此 ES6 promise 链?

mysql - 一旦连接限制达到npm mysql就无法获取新连接

json - 如何通过JSON输出Jade内容? ( Node .js)

node.js - 使用 docker-compose 设置带有 redis 的 Node

javascript - 如何使用 MongoDB 和 Nodejs 创建用户角色/权限?

foreign-keys - 删除级联上的 MongoDB DBRef