Meteor - 根据用户状态发布集合

时间:2016-02-25 23:08:26

标签: meteor

我正在尝试为拥有accountStatus.isUserAccountActive: true的用户发布类集合。我当前的代码发布了活动的用户,然后是整个类集合。有人可以告诉我我做错了吗?

Meteor.publish('classList', function (group) {
  if (Roles.userIsInRole(this.userId, ['is_teacher'], group)) {
    return [
    Meteor.users.find({roles:'is_student', "accountStatus.isUserAccountActive": true}, 
      {fields: {
        "profile.firstName": 1, 
        "profile.familyName": 1,
        roles: 1, 
        "accountStatus.isUserAccountActive": 1
      }
    }),
    classes.find({teacherUserId: this.userId},
      {fields: {
        "studentUserId": 1,
        "class": 1
      }})
    ];
  } else {

    // user not authorized. do not publish secrets
    this.stop();
    return;

  }
});

1 个答案:

答案 0 :(得分:1)

我相信这会让你得到你想要的东西:

Meteor.publish('classList', function (group) {
  if (Roles.userIsInRole(this.userId, ['is_teacher'], group)) {
    var users = Meteor.users.find({roles:'is_student', "accountStatus.isUserAccountActive": true}, 
      {fields: {
        "profile.firstName": 1, 
        "profile.familyName": 1,
        roles: 1, 
        "accountStatus.isUserAccountActive": 1
      }
    }); // Get active students.

    // Get the user ids of active students.
    var userIdsWhoAreActive = users.fetch().map( function( user ) {
        return user._id;
    });
    return [ users,
    classes.find({teacherUserId: this.userId, studentUserId: { $in: userIdsWhoAreActive },
      {fields: {
        "studentUserId": 1,
        "class": 1
      }})
    ];
  } else {

    // user not authorized. do not publish secrets
    this.stop();
    return;

  }
});

在英语中,我认为第二个查询是:"给我所有的课程,其中teacherUserId是当前用户的id,而studentUserId是在活跃的学生用户数组中#34;