【问题标题】:password confirmation with sails.js使用sails.js 确认密码
【发布时间】:2014-07-25 18:13:24
【问题描述】:

我正在尝试在sails 0.10.0-rc9 中设置密码确认。这是用于新用户注册的。当我在两个字段中使用相同的密码时,控制台确认它正在获取相同的密码,然后吐出它们不匹配的错误。我想知道我错过了什么......

模型 - user.js - 看起来像 https://github.com/mrcn/C_Express/blob/master/api/models/User.error.js

module.exports = {
  schema: true,
  attributes: {
    name: {
      type: "string",
      required: true
    },
    email: {
      type: "string",
      email: true,
      required: true,
      unique: true
    },
    encryptedPassword: {
      type: "string"
    },
    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      delete obj.confirmation;
      delete obj.encryptedPassword;
      delete obj._csrf;
      return obj;
    }
  },
  beforeCreate: function (values, next) {
      console.log("Called beforeCreate User ");
      console.log(values);
    if(!values.password || values.password != values.confirmation) {
      console.log("\n if statement call \n");
      return next({
        err : ["password dont match."]
      });
    }
  }
};

表格如下所示: https://github.com/mrcn/C_Express/blob/master/views/user/new.ejs

  <input type="text" class="form-control" placeholder="your name" name="name" required />

  <input type="email" class="form-control" placeholder="email address" name="email" data-parsley-trigger="change" required />

  <input type="password" class="form-control" placeholder="password" name="password" required />

  <input type="password" class="form-control" placeholder="confirmation" name="confirmation" required />
  <br />

  <input type="submit" class="btn btn-lg btn-primary btn-block" value="Create Account" />
  <input type="hidden" name="_csrf" value="<% _csrf %>" />

当我对所有内容使用随机“wer”时,控制台日志”

Called beforeCreate User
{ name: 'wer',
  email: 'wer@wer.com',
  password: 'wer',
  confirmation: 'wer',
  _csrf: '',
  id: null }

 if statement call

{ err: [ 'password dont match.' ] }

【问题讨论】:

  • 你能显示你看到的日志吗?此外,您还需要在成功案例中调用next(),否则您的模型将永远无法创建...
  • 我用控制台日志更新了帖子。为了首先解决这个问题,我从模型中删除了所有其他内容。
  • 呃,有点不对劲。您确定您发布的代码是您的应用程序中实际拥有的代码吗?它对我来说很好。
  • 当我修复错字错误时,我将它保存到另一个文件中。 stackoverflow,请原谅我
  • 它发生了。您可以随时删除您的帖子。

标签: javascript node.js sails.js


【解决方案1】:

也许password 在两个地方拼写一样? :P

if(!values.password || values.pasword != values.confirmation) {
                                ^oops

【讨论】:

    【解决方案2】:

    beforeCreate 更改为 beforeValidate Ç

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2018-12-25
      相关资源
      最近更新 更多