2

NodeJS 用の Restify と Mongoose を使用して 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();
        }
    },
4

1 に答える 1

1

この問題の原因の 1 つは、pre save hookエラーが黙って発生する場合です。

モデルが.pre('save' () => {...})関数として見つかった場合は、 を呼び出した後にこのメソッドに到達しsave、エラーなしで返されることを再確認してください。

マングース ミドルウェアに関するドキュメントは、ここにあります。

于 2015-09-25T13:01:37.840 に答える