Javascript Geddy保存用户

Javascript Geddy保存用户,javascript,node.js,geddy,Javascript,Node.js,Geddy,我目前正在帮助一个使用Geddy js框架的项目,这是我第一次使用它。我目前正在尝试修复用户模型中的create方法。代码如下: this.create = function (req, resp, params) { var self = this , user = geddy.model.User.create(params); //need to ensure that the user agrees with the terms and conditions

我目前正在帮助一个使用Geddy js框架的项目,这是我第一次使用它。我目前正在尝试修复用户模型中的create方法。代码如下:

this.create = function (req, resp, params) {
    var self = this
      , user = geddy.model.User.create(params);

    //need to ensure that the user agrees with the terms and conditions.

    // Non-blocking uniqueness checks are hard
    geddy.model.User.first({username: user.username}, function(err, data) {
      if (data) {
        params.errors = {
          username: 'This username is already in use.'
        };
        //self.transfer('add');
      }
      else {
        if (user.isValid()) {
          user.password = cryptPass(user.password);
          user.suburb = "";
          user.state = "";
          user.postcode = "";
        }
        user.save(function(err, data) {
          if (err) {
            params.errors = err;
            self.transfer('add');
          }
          else {
              // setup e-mail data with unicode symbols
              var mailOptions = {
                  from: "App ✔ <hello@app.com>", // sender address
                  to: user.email, // list of receivers
                  subject: user.username + " Thank you for Signing Up ✔", // Subject line
                  text: "Please log in and start shopping! ✔", // plaintext body
                  html: "<b>Please log in and start shopping!✔</b>" // html body
              }

              smtpTransport.sendMail(mailOptions, function(error, response){
                  if(error){
                      console.log(error);
                  }else{
                      console.log("Message sent: " + response.message);
                  }

                  // if you don't want to use this transport object anymore, uncomment following line
                  smtpTransport.close(); // shut down the connection pool, no more messages
              });
            self.redirect({controller: self.name});
          }
        });
      }
    });
};
this.create=函数(请求、响应、参数){
var self=这个
,user=geddy.model.user.create(params);
//需要确保用户同意条款和条件。
//非阻塞唯一性检查很困难
geddy.model.User.first({username:User.username},函数(err,data){
如果(数据){
参数错误={
用户名:“此用户名已被使用。”
};
//自行转让(“添加”);
}
否则{
if(user.isValid()){
user.password=cryptPass(user.password);
user.substance=“”;
user.state=“”;
user.postcode=“”;
}
user.save(函数(错误、数据){
如果(错误){
params.errors=err;
自行转让(“添加”);
}
否则{
//使用unicode符号设置电子邮件数据
var mailpoptions={
发件人:“应用程序✔ ", // 发件人地址
收件人:user.email,//收件人列表
主题:user.username+“感谢您的注册✔“,//主题行
文字:“请登录并开始购物!✔“,//纯文本正文
html:“请登录并开始购物!✔“//html正文
}
发送邮件(邮件选项,函数(错误,响应){
如果(错误){
console.log(错误);
}否则{
console.log(“发送的消息:+response.Message”);
}
//如果不想再使用此传输对象,请取消对以下行的注释
smtpTransport.close();//关闭连接池,不再发送消息
});
重定向({controller:self.name});
}
});
}
});
};
如果查看代码,显然会检查所谓的用户是否有效,例如:
If(user.isValid()){
user.password=cryptPass(user.password);
user.substance=“”;
user.state=“”;
user.postcode=“”;
}

不管用户是否有效,都会继续“保存”。我在想为什么代码是这样的?听起来很荒谬。我问了项目的原始开发人员,他说模型显然是在他创建项目时生成的

因此,在有点困惑的状态下,如果有人能告诉我为什么save方法首先在if语句之外?这是Geddy的原始创建者想要的吗?还是真的很荒谬,我应该改变它

谢谢。

Geddy的
save()
调用将在数据无效时出错(除非设置了force标志,事实并非如此)。它实际上使用了相同的
isValid()
调用。因此,看起来您这里的方法只是有人用一个错误处理程序来处理所有错误情况


对于
user.password
仅在数据看起来有效时才使用加密数据进行设置,我猜这只是为了使“必须设置”类型的验证生效。很有可能,即使使用空密码,加密字符串也会被视为已设置。

user.isValid做了什么?可能是设置了一个
user.save
在返回错误之前进行计算。也可能是出于设计,它总是会保存。当然,这看起来很奇怪,但它对有效和无效用户是否能像预期的那样工作?显然,这是Geddy验证模型及其属性的方法。对不起,我对Geddy不太熟悉,但看起来确实有人希望返回错误如果用户无效。