Meteor cursor ignores sort option

I use Meteorwith Reactand react-meteor-data. I am trying to get a function Videos.find(...)to return an array sorted by 'index' field. See my code below.

The first console.log()prints an array that is correctly sorted. The second console.log()prints an array by default. Even if I changed the sort option, it seems to be ignored. I can’t figure out how to sort the constant videos.

export default DashboardContainer = createContainer(props => {
  const videosHandle = Meteor.subscribe('userVideos');
  const loading = !videosHandle.ready();
  const videos = !loading ? Videos.find({}, {fields: {title:1, subtitle:1,
    duration:1, thumb:1, url:1, index:1, groups:1}, sort: {index:1}}).fetch() : [];

  // logs the array properly sorted by the index field of the elements
  console.log(Videos.find({}, {fields: {title:1, subtitle:1, duration:1, thumb:1, url:1, index:1, groups:1}, sort: {index:1}}).fetch());

  // logs the array unsorted (unwanted behaviour)
  console.log(videos);

  return {
   loading,
   videos,
  };
}, Dashboard);

Update:

If I use console.log(videos.map((i) => i.index));instead console.log(videos), I get a list printed in the correct order:

[1,2,3,4,5...]

Why?

+4
source share

No one has answered this question yet.

:

3428
?
2466
string
1687
?
1544
<, >
1251
1064
<T>
995
?

Source: https://habr.com/ru/post/1657174/


All Articles