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

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).

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.

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.

Good points! I should modify this then.

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

3 Likes