bp123
bp123

Reputation: 3417

Sorting users using date from separate collection

I'm trying to get return a list of users based on their accountActiveDate which is stored in a separate collection. If I console.log candidateUserIDByDate it returns the users in the correct order however when I return Meteor.users.find using the var candidateUserIDByDate its not sorted.

Path: sort.js

let sortCandidatesByDate = CandidateAccountStatus.find({}, {sort: {accountActiveDate: 1}}).fetch();
let candidateUserIDByDate = _.map(sortCandidatesByDate, function(obj) {
  return obj.candidateUserId;
});

return Meteor.users.find({$and: [{_id: { $ne: Meteor.userId() }}, {_id: { $in: candidateUserIDByDate }}]});

Upvotes: 0

Views: 34

Answers (1)

Luna
Luna

Reputation: 1178

I think a (probably hackish) solution would be, returning the CandidateAccountStatus and inside the loop, using another helper to return the correct user like this:

Template helpers:

status: function(){
    //you might want to do {$ne: {Meteor.userId()}} for the correct field
    //in your selector if you don't want currentUser status
    return CandidateAccountStatus.find({}, {sort: {accountActiveDate: 1}})
},

statusUser: function(){
    //change correctField to how you save the userId to
    //your CandidateAccountStatus schema
    return Meteor.users.findOne({_id: this.correctField})
}

HTML

{{#each status}}
    {{#with statusUser}}
        <!-- You have the user object here -->
        {{_id}} <!-- gives the userId -->
    {{/with}}
{{/each}}

Upvotes: 1

Related Questions