Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 仅在Sails模型中存储必要的数据_Node.js_Mongodb_Sails.js_Data Modeling_Waterline - Fatal编程技术网

Node.js 仅在Sails模型中存储必要的数据

Node.js 仅在Sails模型中存储必要的数据,node.js,mongodb,sails.js,data-modeling,waterline,Node.js,Mongodb,Sails.js,Data Modeling,Waterline,我是新手,在模型方面遇到了一个小问题。 我定义了一个用户模型,如下所示: module.exports = { attributes: { firstName: { type: 'string' }, lastName: { type: 'string' }, email: { type: 'email', required: true }, password: { type: 'String

我是新手,在模型方面遇到了一个小问题。
我定义了一个用户模型,如下所示:

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'
   }
 }
};  
我还有一个叫做Plan的模型,它的外键是user:

module.exports = {
   planId: { type: 'string'},
   userId: { model: 'User'}
};  
现在,Plan存储所有用户数据。是否有任何方法可以限制计划模型只保存一些用户详细信息,如名字、姓氏、电子邮件和项目成员,而不存储其他个人信息。比如密码、密码等等


提前感谢

计划没有存储用户数据,它只存储对用户模型中用户数据的引用。

计划模型不会存储用户数据。它将只存储在其模式中定义的数据值,即planId和userId。如果只想返回一些用户详细信息,则可以执行以下操作:

function getUserData(){   
 Plan
    .find()
    .populate('userId')
    .then(function(data){
      return data;
    })
}
在计划模型中:

首先在模型中定义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());
})