Session.set() an array of values vs using client-only Collection?


#1

I have a form where I’ve got this at the bottom:

{{#each formErrors}}
  <li>{{error}}</li>
{{/each}}

When validating the form, I’m using basic Javascript Array.push() to push all the errors into an array, then do Session.set('formErrors', errArray) and then the helper returns that.

Is this any better or worse than using a client-only collection?

formErrors = new Mongo.Collection(null);

and using insert operations to populate the collection? And once the form is corrected, the formErrors collection is emptied (but not destroyed).


#2

I think both will work fine. Personally I prefer using a client-only collection so that I can run queries to get only some of the form errors if I need to.


#3

Depending on what you do with the errors and how you display them, a collection would be preferable in cases you don’t want to invalidate the whole errors array. With a collection, only the change will be reflected whereas with a session tht contains an array, any change in the array will mean a change on the array as a whole.


#4

Good points! I should modify this then.


#5

One aspect of local collections to be aware of is that is does not survive hot code reload.