Sort collection - using schema


#1

Hi

I’ve started using schema for the first time and i’m writing a chat app in which i want the latest message to be displayed ontop, alas i need to sort the collection.

Template.messages.created = function () {
  this.autorun(function () {
    Meteor.subscribe('messages');
  }.bind(this));
};

Template.messages.helpers({
  messages: Messages.find({}, {sort: {date_created: -1}})
});

Messages.attachSchema(new SimpleSchema({ //take this from docs.
  text: {
    type: String,
    label: "Medelande",
    max: 200
  }
//Date should go here 
}));

If i add a date field in the schema, it ends up displaying a input. How should i approach this?


#2

I am assuming you are using autoform to render the form for posting messages. Look up the options there, you can specify which fields it should omit and which it should create form elements for. Then you can add your date field and it won’t show for the user. And you can use autoValue from autoform (or one of those packages) in order to automatically fill it with the current date.

HTH!