Meteor.js: Find all documents and return in reverse natural order

前端 未结 4 776
栀梦
栀梦 2021-01-05 05:10

I am trying to return all documents in a collection, to use it with an {{#each}} in my template. My code looks like this:

return Answers.find({}, {sort: {$nat

4条回答
  •  滥情空心
    2021-01-05 05:33

    Can't tell why don't it returns in reverse order.

    But you can create an array in the template helper method and return reverse of an array using array.sort() or array.reverse() functions.

    For ex: Say you Answers collection looks like this:

    Answers({ansNo: 1, ansBody: "body1"},
            {ansNo: 2, ansBody: "body2"},
            {ansNo: 3, ansBody: "body3"});
    

    And the array to be returned is:

    var AnswersArr = new Array();
    

    then in your template helper :->

    var tempCollection = Answers.find({});
    tempCollection.forEach(function(data){
        var obj = {ansNo: data.asnNo, ansBody: data.ansBody};
        AnswersArr.push(abj);
    });
    
    AnswersArr.sort(function(a, b){return b.ansNo - a.ansNo;});  //sort in reverse order
    
    return AnswersArr;
    

提交回复
热议问题