1

I have been following many tutorials concerning the MEAN stack, and I have come to a question I've found hard to answer (note: I found a possible duplicate when searching, but don't believe it answers my question)

Take this code for example, which can be found here

// app/models/nerd.js
// grab the mongoose module
var mongoose = require('mongoose');
// define our nerd model
// module.exports allows us to pass this to other files when it is called
module.exports = mongoose.model('Nerd', {
  name : {type : String, default: ''}
});

After finishing the tutorial, I rewrote the code to attempt to replicate the lessons like so ("menu item" for a restraunt):

var mongoose = require('mongoose');

module.exports = mongoose.model('Item', {
  name: {type: String, required: true},
  description: {type: String, require: true},
  price: {type: String, required: true},
  ingredients: [{
    name: {type: String, default: ''},
    amt: {type: String, enum: ['None', 'Reg', 'XTRA'], required: true}
  }]
});

I will be using an interface to create new menu items. Should I leave the code as is, or use a Schema?

Community
  • 1
  • 1
h3xc0ntr0l
  • 399
  • 1
  • 3
  • 14

1 Answers1

0

Well. I was reading through code to post as an example for a difference, and came to this.

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var BearSchema   = new Schema({
    name: String
});

module.exports = mongoose.model('Bear', BearSchema);

I then noticed that the schema was defined inline (I think?) instead of being declared. I would assume that this is because later, it will be much cleaner for me to add more schema declarations in this model, along with methods. If anyone can provide me with clarification, I'll give you the answer!

h3xc0ntr0l
  • 399
  • 1
  • 3
  • 14
  • I was following the same tutorial on scotch, and i just want to know, what would be the expected Nerd data json? Like: **{ "_id" : ObjectId("5b965b3a15da33b567d9f794"), "name" : "Tom" }** – shasi kanth Sep 10 '18 at 11:57