为何' encryptedPassword'参数在sails.js项目中被解释为未定义?

时间:2015-08-04 00:24:08

标签: node.js sails.js

我正在使用irlnathan / ponzicoder的Sailscasts系列作为指南,开发一个网络应用程序。我已经为{'用户'}创建了条目Ep. 5。使用注册动作的模型。在本教程的这个阶段(链接的精确时间),我们看到他收到的响应页面是刚刚使用注册创建的User的JSON对象。但是,我在我的回复页面中得到以下信息:

{
  "error": "E_VALIDATION",
  "status": 400,
  "summary": "1 attribute is invalid",
  "model": "User",
  "invalidAttributes": {
    "encryptedPassword": [
      {
        "rule": "string",
        "message": "`undefined` should be a string (instead of \"null\", which is a object)"
      },
      {
        "rule": "required",
        "message": "\"required\" validation rule failed for input: null"
      }
    ]
  }
}

这表明我的加密密码'在我返回响应时,参数以某种方式未定义。但是,当我记录json对象时,我将返回到我的控制台中的响应,我得到以下内容(这是我期望看到的而不是E_VALIDATION响应):

{ firstName: 'First',
  lastName: 'Last',
  email: 'email@domain.com',
  encryptedPassword: '$2a$10$q0TfKWnN47mucsZLybN8WeygPPMpYxp9VMGUgIjA./ipZBn.POuOG',
  lastLoggedIn: '2015-08-03T23:45:22.520Z',
  gravatarUrl: 'http://www.gravatar.com/avatar/7328fddefd53de471baeb6e2b764f78a?',
  createdAt: '2015-08-03T23:45:22.523Z',
  updatedAt: '2015-08-03T23:45:22.523Z',
  id: 39 }

所以encryptedPassword肯定是正确创建和定义的。

这是一个类似于 THIS 用户面临的问题,除了我的问题不在于缺少HTML名称属性。更奇怪的是,我的问题特别在于加密密码'参数,没有其他参数。

我必须承认,由于我没有使用与我的注册表单完全相同的字段作为教程,因此可能存在一些导致问题的差异。但是,经过几个小时的调试后,我仍然无法弄明白。这就是我使用

的内容

相关代码:

注册操作(UserController.js< - 遵循sails.js约定)

作为注册函数中散布的每个console.log语句的注释,我得到了所有的非null值。我这样做是为了验证确实encryptedPassword不是null / undefined

/**
 * UserController
 *
 * @description :: Server-side logic for managing Users
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {

    signup: function(req, res) {
        var Passwords = require('machinepack-passwords');

        // Encrypt a string using the BCrypt algorithm.
        Passwords.encryptPassword({
            password: req.param('password')
            // difficulty: 10,
        }).exec({
            // An unexpected error occurred.
            error: function (err){
                return res.negotiate(err);
            },
            // OK.
            success: function (encryptedPassword){
                console.log("1", encryptedPassword);

                require('machinepack-gravatar').getImageUrl({
                    emailAddress: req.param('email')
                }).exec({
                    error: function(err) {
                        return res.negotiate(err);
                    },
                    success: function(gravatarUrl) {
                        console.log("2", encryptedPassword);
                        console.log("2", gravatarUrl);

                        User.create({
                            firstName: req.param('firstName'),
                            lastName: req.param('lastName'),
                            email: req.param('email'),
                            encryptedPassword: encryptedPassword,
                            lastLoggedIn: new Date(),
                            gravatarUrl: gravatarUrl
                        }, function userCreated(err, newUser) {
                            console.log("NEWUSER", newUser);
                            console.log("3", encryptedPassword);
                            console.log("3", gravatarUrl);

                            if (err) {
                                console.log("err: ", err);
                                console.log("err.invalidAttributes: ", err.invalidAttributes)

                                // If this is a uniqueness error about the email attribute,
                                // send back an easily parseable status code.
                                if (err.invalidAttributes && err.invalidAttributes.email && err.invalidAttributes.email[0]
                                    && err.invalidAttributes.email[0].rule === 'unique') {
                                    return res.emailAddressInUse(); //some error that I have already defined somewhere else in my project
                                }

                                return res.negotiate(err);
                            }

                            return res.json({
                                id: newUser.id
                            });
                        });
                    }
                });
            }
        });
    }
};

用户模型的属性(User.js< - 遵循sails.js约定)

/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

    schema: true,

    attributes: {

        //all the other attributes

        // The encrypted password for the user
        encryptedPassword: {
            type: 'string',
            required: true
        }

        //more attributes

    }

};

注册表单的HTML

<form ng-submit="submitSignupForm()" id="sign-up-form" name="signup" class="form" role="form">

  <!-- irrelevant html -->

  <!-- P A S S W O R D -->
  <div class="control-group form-group" ng-class="{'has-error':signup.password.$invalid && signup.password.$dirty}">
    <input type="password" name="password" value="" class="form-control input-lg" placeholder="Password" ng-model="signupForm.password" id="password" required ng-minlength="12"/>
    <span class="help-block has-error" ng-if="signup.password.$dirty">
      <span ng-show="signup.password.$error.required">Password is required.</span>
      <span ng-show="signup.password.$error.minlength">Password must be at least 12 characters.</span>
    </span>
  </div>

  <!-- more misc html -->

</form>

和注册控制器

angular.module('SignupModule').controller('SignupController', ['$scope', '$http', 'toastr', function($scope, $http, toastr) {

    $scope.signupForm = {
        loading: false
    }

    $scope.submitSignupForm = function() {
        $scope.signupForm.loading = true;

        console.log("clicked!");

        $http.post('/signup', {
            firstName: $scope.signupForm.firstName,
            lastName: $scope.signupForm.lastName,
            email: $scope.signupForm.email,
            password: $scope.signupForm.password
        })
        .then(function onSuccess() {
            window.location = '/user';
        })
        .catch(function onError(sailsResponse) {
            var emailAddressAlreadyInUse = (sailsResponse.status == 409);
            if (emailAddressAlreadyInUse) {
                toastr.error('That email address has already been taken, please try again.', 'Error');
                return;
            }
        })
        .finally(function eitherWay() {
            $scope.signupForm.loading = false;
        });
    }

}]);

重申一下,为什么地球上的encryptedPassword被解释为null?

0 个答案:

没有答案