Create and Save a Record of a Model
Hints
Hint 1
You need to do the following:
- Create a model of a person, using the schema from exercise 2
- Create a new person, including their attributes
- Save the new person you created
- Put your new person inside the
createAndSavePerson
function
Solutions
Solution 1 (Click to Show/Hide)
Code for myApp.js
/** 1) Install & Set up mongoose */
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);
/** 2) Create a 'Person' Model */
var personSchema = new mongoose.Schema({
name: String,
age: Number,
favoriteFoods: [String]
});
/** 3) Create and Save a Person */
var Person = mongoose.model('Person', personSchema);
var createAndSavePerson = function(done) {
var janeFonda = new Person({name: "Jane Fonda", age: 84, favoriteFoods: ["eggs", "fish", "fresh fruit"]});
janeFonda.save(function(err, data) {
if (err) return console.error(err);
done(null, data)
});
};