2015-12-18 78 views
0

這是我試圖將多個發現鏈接在一起的第一次嘗試。調試運行表明,所有的代碼都能正確執行,但在接收用戶數組時會有延遲,因此無法將數據返回。多個鏈接的承諾是sailsjs

該概念是用戶可能屬於多個組織,並且可能有多個用戶(除當前用戶之外)可能屬於組織。該功能試圖接收當前用戶所屬的所有組織的所有用戶。

getUserOrganizationsUsers: function (userId) { 
    var users = []; 

    sails.log.info('Getting the current users organizations [' + userId + ']'); 
    return UserOrganization.find({ user_id: userId, deleted: null }) 
     .populate('organization_id', { deleted: null }) 
     .populate('user_id', { deleted: null }) 
     .then(function (userorganization) { 
     return userorganization; 
     }) 
     .then(function (userorgs) { 
      /* From all the organizations I want to get all the users from those organizations */ 
     _.forEach(userorgs, function (userorg) { 
      UserOrganization.find({ organization_id: userorg.organization_id.id }) 
      .populate('organization_id', { deleted: null }) 
      .populate('user_id', { deleted: null }) 
      .then(function (otherusrs) { 
       _.forEach(otherusrs, function (otherusr) { 
       sails.log.info('other userss each loop '); 
       var users = _.find(otherusrs, {id: otherusr.organization_id.id}); 
       users.push(users); 
       }) 
      }) 
     }); 

     return Q.when(employees); 
     }) 
    }, 

Organization.js

module.exports = { 
    attributes: { 
    companyName: { 
     type: 'string', 
     required: true 
    }, 
    Address: { 
     type: 'string' 
    }, 
ABN: { 
     type: 'string' 
    }, 
    City: { 
     type: 'string' 
    }, 
    contactNumber: { 
     type: 'string' 
    }, 
    country: { 
     type: 'string' 
    }, 
    icon: { 
     type: 'string' 
    }, 
     users: 
{ collection: 'userorganization', 
    via : 'user_id' 
}, 
     deleted: { 
     type: 'date', 
     defaultsTo: null 
    }, 
    toJSON: function() { 
     var obj = this.toObject(); 
     obj = _.pick(obj, Organization.publicFields); 
     return obj; 
    } 
    }, 

    editableFields: [ 
    'companyName', 
'users' 
// 'industries' 
    ], 

    publicFields: [ 
    'id', 
    'companyName', 
'users' 
    ], 

    }; 

UserOrganization.js

module.exports = { 

    attributes: { 
    organization_id: { 
     model : 'organization', 
     required: true 
    }, 
    user_id: { 
     model: 'user', 
     required: true 
    }, 
    organizationRole: { 
     type: 'string', 
     required: true 
    }, 
    deleted: { 
     type: 'date', 
     defaultsTo: null 
    }, 
    toJSON: function() { 
     var obj = this.toObject(); 
     obj = _.pick(obj, UserOrganization.publicFields); 
     return obj; 
    } 
    }, 

    editableFields: [ 
    'organization_id', 
    'user_id', 
    'organizationRole', 
    ], 

    publicFields: [ 
    'id', 
    'organization_id', 
    'user_id', 
    'organizationRole' 
    ], 

}; 

和user.js的

var bcrypt = require('bcrypt-nodejs'); 

module.exports = { 
    attributes: { 
    email: { 
     type: 'email', 
     required: true, 
     unique: true 
    }, 
    password: { 
     type: 'string', 
     required: true 
    }, 
     firstName: { 
     type: 'string' 
    }, 
    lastName: { 
     type: 'string' 
    }, 
    verified: { 
     type: 'boolean', 
     defaultsTo: false 
    }, 
    organizations: 
{ collection: 'userorganization', 
    via : 'user_id' 
},  deleted: { 
     type: 'date', 
     defaultsTo: null 
    }, 
    fullName: function() { 
     return this.firstName + ' ' + this.lastName; 
    }, 
    toJSON: function() { 
     var obj = this.toObject(); 
     obj = _.pick(obj, User.publicFields); 
     return obj; 
    } 
    }, 

    // TODO: Add initialFields 

    editableFields: [ 
    'password', 
    'email', 
    'firstName', 
    'lastName', 
    'organizations'], 

    publicFields: [ 
    'id', 
    'email', 
    'verified', 
     'firstName', 
    'lastName', 
     'fullName', 
     'organizations' 
    ], 

    comparePassword: function (password, user, cb) { 
    bcrypt.compare(password, user.password, function (err, match) { 
     if(err) return cb(err); 
     cb(null, match); 
    }) 
    }, 

    beforeCreate: function (user, cb) { 
    bcrypt.genSalt(10, function (err, salt) { 
     bcrypt.hash(user.password, salt, function() {}, function (err, hash) { 
     if (err) { 
      sails.log.error(err); 
      return cb(err); 
     } 
     user.password = hash; 
     cb(null, user); 
     }); 
    }); 
    } 
}; 

回答

0

好吧,我想明白你的」再見卷板機。讓用戶直接屬於某個組織將會簡單得多。

不管怎麼說,如果我理解正確的模型結構,這樣的事情應該工作:

getUserOrganizationsUsers: function (userId) { 

    UserOrganization.find({ user_id: userId, deleted: null }) 
     .then(function (userOrgs) { 
     // return array of organization IDs 
     return _.map(userOrgs, function(org){ 
      return org.id; 
     }); 
     }) 
     .then(function (userOrgs) { 
     Organization.find(userOrgs) 
      .populate('users') // users is a collection of UserOrganization 
      .exec(function(err, orgs){ // lookup organizations 
      if(err) //handle error 
      else { 
       return _.flatten(// return basic array for next promise handler 
        _.map(orgs, function(org){ // for each organization 
         return _.map(org.users, function(user){ // return an array of user_ids 
          return user.user_id; 
         }) 
        }) 
       ) 
      } 
     }) 
     }) 
     .then(function(allUserOrgs){ 
      UserOrganization.find(allUserOrgs) 
      .populate('user_id') 
      .exec(function(err, userOrgsList){ 
       return _.map(userOrgsList, function(user){ 
        return user.user_id; 
       }) 
      }) 
     }) 
     .then(function(users){ 
      // users should be an array of all the users form allt he organizations that the current users belongs to 
     }) 
},