Transforming Collection Documents

Transform collection documents with a custom function.

Good job! That's it for "Transforming Collection Documents."

Need a second look? Watch again

Transforming Collection Documents

The transform option on Meteor Collections allows us to transform MongoDB documents before they're returned in a fetch, findOne or find call, and before they are passed to observer callbacks. It lays the foundation for a Model layer. In this episode I'll build a simple transform class that has a formatPrice method for a price that is stored as cents in the database.

  • Meteor
  • Tutorial
<p class="code-block-title">app.js</p> ```javascript Product = function (doc) { _.extend(this, doc); }; Product.prototype = { constructor: Product, formatPrice: function (format) { switch(format) { case "cents": return this.price; default: return (this.price / 100).toFixed(2); } } }; ProductCollection = new Meteor.Collection("products", { transform: function (doc) { return new Product(doc); } }); ```
Join the Discussion

Want to leave a comment? .