Fork me on GitHub

[

Mongoose ](../index.html)

Mongoose in the browser

As of v3.9.3, Mongoose schema declarations are isomorphic, that is, you can use mongoose's browser component to validate objects against your mongoose schemas in the browser.

To include mongoose in your browser code, you can use require('mongoose') if you are using Browserify or you can include mongoose in a script tag. The below example utilizes mongoose's Amazon CloudFront CDN.

If you are using webpack to bundle your client-side code, you need to add the following plugin to webpack's plugins array so that webpack does not attempt to resolve mongoose's server-side dependencies:

new webpack.DefinePlugin({ 'typeof window': '\"object\"' })

  1. <script type="text/javascript" src="//d1l4stvdmqmdzl.cloudfront.net/4.0.2/mongoose.js">
  2. </script>

Declaring schemas in the browser

When you include the mongoose.js file in a script tag, mongoose will attach a mongoose object to the global window. This object includes a Schema constructor that you can use to define schemas much like in NodeJS.

Note: Mongoose's browser component requires an ECMAScript 5 compliant browser. In particular, it will not work in Internet Explorer 8 or Safari 5.

allows you to use mongoose types

  1. var foodSchema = new mongoose.Schema({name: String});
  2. var breakfastSchema = new mongoose.Schema({
  3. foods: [foodSchema],
  4. date: {type: Date, default: Date.now}
  5. });
  6. assert.ok(foodSchema.path('name') instanceof mongoose.Schema.Types.String);
  7. assert.ok(breakfastSchema.path('foods') instanceof mongoose.Schema.Types.DocumentArray);
  8. assert.ok(breakfastSchema.path('date') instanceof mongoose.Schema.Types.Date);

Validating documents in the browser

The primary goal of mongoose's browser component is to validate documents against a given schema. Because the mongoose browser component doesn't currently support any sort of querying, you're responsible for creating your own documents.

allows you to create a schema and use it to validate documents

  1. var schema = new mongoose.Schema({
  2. name: {type: String, required: true},
  3. quest: {type: String, match: /Holy Grail/i, required: true},
  4. favoriteColor: {type: String, enum: ['Red', 'Blue'], required: true}
  5. });
  6. /* `mongoose.Document` is different in the browser than in NodeJS.
  7. * the constructor takes an initial state and a schema. You can
  8. * then modify the document and call `validate()` to make sure it
  9. * passes validation rules. */
  10. var doc = new mongoose.Document({}, schema);
  11. doc.validate(function(error) {
  12. assert.ok(error);
  13. assert.equal('Path `name` is required.', error.errors['name'].message);
  14. assert.equal('Path `quest` is required.', error.errors['quest'].message);
  15. assert.equal('Path `favoriteColor` is required.',
  16. error.errors['favoriteColor'].message);
  17. doc.name = 'Sir Lancelot of Camelot';
  18. doc.quest = 'To seek the holy grail';
  19. doc.favoriteColor = 'Blue';
  20. doc.validate(function(error) {
  21. assert.ifError(error);
  22. doc.name = 'Sir Galahad of Camelot';
  23. doc.quest = 'I seek the grail'; // Invalid, must contain 'holy grail'
  24. doc.favoriteColor = 'Yellow'; // Invalid, not 'Red' or 'Blue'
  25. doc.validate(function(error) {
  26. assert.ok(error);
  27. assert.ok(!error.errors['name']);
  28. assert.equal('Path `quest` is invalid (I seek the grail).',
  29. error.errors['quest'].message);
  30. assert.equal('`Yellow` is not a valid enum value for path `favoriteColor`.',
  31. error.errors['favoriteColor'].message);
  32. done();
  33. });
  34. });
  35. });