我目前正在参与一个涉及使用 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});
}
});
}
});
};
如果您查看代码,显然会检查所谓的用户是否有效,如下所示:if (user.isValid()) {
user.password = cryptPass(user.password);
user.suburb = "";
user.state = "";
user.postcode = "";
}
无论用户是否有效,都会继续“保存”。我在想为什么代码是这样的?听起来很荒谬。我询问了该项目的原始开发人员,他说模型显然是在他创建项目时生成的。
所以在有点困惑的状态下,如果有人能告诉我为什么 save 方法首先在 if 语句之外?这是 Geddy 的原始创造者想要的吗?还是真的很荒谬,我应该改变它?
谢谢。