2014-11-04 16 views
0

我是Sails的新手,面臨模型的一個小問題。
如下我定義一個用戶模型:只在Sails模型中存儲必要的數據

module.exports = { 
    attributes: { 
    firstName: { 
    type: 'string' 
    }, 
    lastName: { 
     type: 'string' 
    }, 
    email: { 
    type: 'email', 
    required: true 
    }, 

    password: { 
    type: 'String' 
    }, 
    passwordSalt: { 
    type: 'String' 
    }, 
    projects:{ 
    collection: 'ProjectMember', 
    via: 'userId' 
    } 
} 
}; 

我有一個叫計劃多個模型具有用戶作爲其外交重點:

module.exports = { 
    planId: { type: 'string'}, 
    userId: { model: 'User'} 
}; 

現在計劃存儲所有用戶數據。有沒有什麼辦法可以限制Plan模型只保留一些用戶的詳細信息,如firstName,lastName,email和projectMembers,而不是存儲其他個人信息。像密碼,passwordSalt等?

由於事先

回答

1

計劃不存儲用戶數據,它僅存儲在用戶模型中找到的用戶數據的引用。

1

計劃模型將不會存儲用戶數據。它只會存儲其模式中定義的數據值,即planId和userId。如果你想返回回只有一些用戶信息,那麼你可以這樣做:

計劃型號:

先在模型中定義一個toApi方法:

module.exports = { 
    attributes : { 
    planId: { type: 'string'}, 
    userId: { model: 'User'}, 
    toApi :toApi 
} 
}; 



function toAPi(){ 
    var plan = this.toObject(); 
    return { 
     firstName : plan.userId.firstName, 
     lastName : plan.userId.lastName, 
     email : plan.userId.email, 
     projectMembers : plan.userId.projectMembers 
    }; 
    } 

,然後在方法,這樣做:

function getUserData(){ 
Plan 
    .find() 
    .populate('userId') 
    .then(function(data){ 
     return data; 
    }) 
} 

在你的計劃控制,做到這一點:

Plan 
.getUserData() 
.then(function(userData){ 
    return res.ok(userData.toApi()); 
}) 
相關問題