嵌套查询在SailsJs中不起作用

时间:2017-07-01 13:25:50

标签: sails.js waterline

我想查询检索“所有具有特定成员的项目”。

Project.js:

module.exports = {

  attributes: {

    name: {
        type: "string"
    },

    topics: {
        collection: "topic",
        via: "projects"
    },

    profesor: {
      type: 'string'
    },

    creator: {
      type: 'string' 
    },

    members: {
        collection: "user",
      via: "projects"
    },

    content: {
        collection: "content",
        via: "projectData"
    }

  }
};

user.js的:

module.exports = {

  attributes: {

    projects: {
      collection:"project",
      via: "members",
      dominant: true
    },

    email: {
        type: 'email',
        required: true,
        unique: true
    },

    mat: {
      type: 'integer',
      required: true,
      unique: true
    },

    name: {
        type: 'string',
        required: true,
    },

    lastname: {
        type: 'string',
        required: true,
    },

    admin: {
      type: 'boolean'
    },


    encryptedPassword : {
        type: 'string'
    },

     toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      delete obj.encryptedPassword;
      delete obj.confirmation;
      delete obj._csrf;
      return obj;
    }

  },


  beforeValidate: function (values, next) {
    console.log(values);
    if (typeof values.admin !== 'undefined') {
      if (values.admin === 'unchecked') {
        values.admin = false;
      } else  if (values.admin[1] === 'on') {
        values.admin = true;
      }
    }
     next();
  },

  beforeCreate: function (values, next) {

    User.count().exec(function countCB(error, found) {
      if (found == 0) {
      values.admin = true;
      }
   });

    // This checks to make sure the password and password confirmation match before creating record
    if (!values.password || values.password != values.confirmation) {
      return next({err: ["Password doesn't match password confirmation."]});
    }

    require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) {
      if (err) return next(err);
      values.encryptedPassword = encryptedPassword;
      // values.online= true;
      next();
    });
  }

};

这是我的疑问:

index: function(req,res){
        Project.find().populate('members', {name: 'Hugo'}).exec(function(err, prj){
                if (err) res.negotiate(err);
                console.log("funciona esto?");
                console.log(prj);


                res.view({
                project: prj
                });
        });
    },

为什么不起作用?它会重建所有项目,任何人都可以帮助我吗?我不确定水线是否允许这样做。

1 个答案:

答案 0 :(得分:0)

尝试获取用户,然后填充项目:

index: function(req,res){
        User
        .find({name: 'Hugo'})
        .populate('projects')
        .exec(function(err, user){
                if (err) res.negotiate(err);
                console.log("funciona esto?");
                console.log(user.projects);


                res.view({
                project: user.projects[0] // first project
                });
        });
    },