在Sails.js中从相关模型访问模型的属性

我使用更新的数据库适配器(用于PostgreSQL)运行Sails.js的testing版(v0.10.0-rc3),以便通过水线ORM具有关联function。 我正在尝试根据不同的访问级别创build一个基于angular色的授权用户模型。 用户到angular色的关联是一对多的。 我的模特是:

API /模型/ user.js的

module.exports = { attributes: { firstName: { type: 'string', required: true }, lastName: { type: 'string', required: true }, fullName: function() { return this.firstName + " " + this.lastName; }, email: { type: 'email', required: true, unique: true }, encryptedPassword: { type: 'string' }, role: { model: 'role' }, groups: { collection: 'group', via: 'users' } }, toJSON: function() { var obj = this.toObject(); delete obj.password; delete obj.confirmation; delete obj._csrf; return obj; }, beforeCreate: function (values, next) { // Makes sure the password and password confirmation match if (!values.password || values.password != values.confirmation) { return next({err: ['Password does not match password confirmation.']}); } // Encrypts the password/confirmation to be stored in the db require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) { values.encryptedPassword = encryptedPassword; next(); }); } }; 

API /模型/ Role.js

 module.exports = { attributes: { name: { type: 'string', required: true, unique: true }, users: { collection: 'user', via: 'role' }, permissions: { collection: 'permission', via: 'roles', dominant: true } } }; 

我知道Waterline不支持通过关联 ,但我仍然可以访问与用户关联的angular色名称,对吗? 例如: user.role.name我现在能够检索angular色名称的唯一方法是对angular色对象进行第二次查询。

为了访问关联的模型,在查询主模型时必须populate关联,例如:

 User.findOne(1).populate('role').exec(function(err, user) { if (err) {throw new Error(err);} console.log(user.role.name); } 

协会文件在这里 。