Sails.js 吃水线+航行0.10rc8/0.10rc9:beforeCreate未按顺序执行
我的模型基于水线:Sails.js 吃水线+航行0.10rc8/0.10rc9:beforeCreate未按顺序执行,sails.js,waterline,Sails.js,Waterline,我的模型基于水线: var Waterline = require('waterline'); var User = Waterline.Collection.extend({..}); 这是我的User.js /** * User.js * * @description :: User Model * @docs :: http://sailsjs.org/#!documentation/models */ var bcrypt = require('bcrypt'); va
var Waterline = require('waterline');
var User = Waterline.Collection.extend({..});
这是我的User.js
/**
* User.js
*
* @description :: User Model
* @docs :: http://sailsjs.org/#!documentation/models
*/
var bcrypt = require('bcrypt');
var crypto = require('crypto');
var Waterline = require('waterline');
var User = Waterline.Collection.extend({
connection: 'mongodb',
attributes: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
email: {
type: 'email',
required: true,
unique: true
},
username: {
type: 'string',
required: true,
unique: true
},
slug: {
type: 'string',
unique: true
},
picUrl: {
type: 'string',
unique: true
},
password: {
type: 'string',
required: true
},
activated: {
type: 'boolean',
defaultsTo: false
},
activationToken: {
type: 'string'
},
// Add a reference to Questions/Stars
starredQuestions: {
collection: 'question',
via: 'star',
dominant: true
},
/**
* Strips the password out of the json
* object before its returned from waterline.
* @return {object} the model results in object form
*/
toJSON: function () {
// this gives you an object with the current values
var obj = this.toObject();
delete obj.password;
delete obj.email;
delete obj.activationToken;
delete obj.activated;
// return the new object without password
return obj;
},
/**
* Adds a method called fullName to the response object
* @return {string} firstName and LastName concat'd
*/
fullName: function () {
return this.firstName + ' ' + this.lastName
}
},
/**
* Hash the users password with bcrypt
* @param {object} user the object of the submitted user data
* @param {Function} cb[err, user] the callback to be used when bcrypts done
*/
beforeCreate: function (user, cb) {
console.log('inside before create');
// create SLUG for better URLs.
if (!user.username) {
return cb({err: ["Must have a username!"]});
}
user.slug = user.username.replace(/\s+/g, '').toLowerCase();
user.username = user.username.toLowerCase();
user.stars = 0;
user.likes = 0;
// Create password + salt
crypto.generate({saltComplexity: 10}, user.password, function (err, hash) {
if (err) {
return cb(err);
} else {
user.password = hash;
user.activated = false; //make sure nobody is creating a user with activate set to true, this is probably just for paranoia sake
user.activationToken = crypto.token(new Date().getTime() + user.email);
return cb(null, user);
}
});
}
});
下面是调用Create方法的代码:
create: function (req, res) {
var params = req.params.all();
User.findOne({'username' : params.username}, function (err, user) {
User.create(params).exec(function (err, user) {
if (err) {
res.send(500, err);
} else {
if (sails.config.user.requireUserActivation) {
var emailTemplate = res.render('email/email.ejs', {user: user}, function (err, list) {
nodemailer.send({
name: user.firstName + ' ' + user.lastName,
from: sails.config.nodemailer.from,
to: user.email,
subject: 'New Account Acivation Required',
messageHtml: list
}, function (err, response) {
sails.log.debug('nodemailer sent', err, response);
});
// seed some questions here.
/*Question.create(params, function (err, question) {
if (err) {
res.send(500, err);
} else {
}
});*/
res.redirect('/success');
});
} else {
res.redirect('/success');
}
}
});
});
},
我在rc8上遇到了这个问题,然后升级到rc9,仍然面临同样的问题。
有什么不对劲
更新:我按照@particlebanana的要求添加了完整的代码,我们需要更多的信息。你能发布你的整个模型吗?谢谢@particlebanana-我刚刚添加了上面要求的代码。吃水线内置于帆中。在Sails中使用模型时,没有必要或建议单独要求吃水线并扩展集合类。只需将module.exports设置为包含连接、属性、beforeCreate等的对象的模型类定义。