我的模型基于水线:
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
的要求添加了完整代码