ebichuhamster
ebichuhamster

Reputation: 236

Sorting by nested value in Meteor?

I have the following function:

function(field,order){
  if(field==="date"){
    return Tasks.find({},{sort:{createdAt:order}});
  }else{
    return Tasks.find({},{sort:{turtlelog.field:order}});
  }
}

The first condition works just fine. But the else body doesn't like that dot notation.

Any idea what is the correct notation?

Upvotes: 2

Views: 318

Answers (1)

ebichuhamster
ebichuhamster

Reputation: 236

Ok, I was pretty close but you guys definitely helped:

By using object literal notation I could not evaluate the field parameter. I used the following:

function(field,order){
  console.log(field);
  console.log(order);
  if(field==="date"){
    return Tasks.find({},{sort:{createdAt:order}});
  }else{
    var orderString = order>0?"asc":"desc";
    var field='turtlelog.'+field;
    var sort = {sort:[field,orderString]}
    return Tasks.find({},sort);
  }
}

I used an alternative notation for the sorting object, and now it's working.

Upvotes: 1

Related Questions