From 3ac898b041bcbaaf9f6268b9aef4519014389070 Mon Sep 17 00:00:00 2001 From: Ai-Lyn Tang Date: Wed, 24 Jul 2019 02:59:12 +1000 Subject: [PATCH] Replace stub with hints and code solution (#36487) * Replace stub with hints and code solution * Change spoiler tag text Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Update `createAndSavePerson` to use callback Previous solution had an empty callback * fix: added hint header --- .../index.md | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/guide/english/certifications/apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model/index.md b/guide/english/certifications/apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model/index.md index 4288f1a80b..02b33361b9 100644 --- a/guide/english/certifications/apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model/index.md +++ b/guide/english/certifications/apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model/index.md @@ -1,10 +1,47 @@ --- title: Create and Save a Record of a Model --- -## Create and Save a Record of a Model +# Create and Save a Record of a Model -This is a stub. Help our community expand it. +## Hints -This quick style guide will help ensure your pull request gets accepted. +### Hint #1 - +You need to do the following: +1. Create a model of a person, using the schema from exercise 2 +2. Create a new person, including their attributes +3. Save the new person you created +4. Put your new person inside the `createAndSavePerson` function + +## Solutions + +
Solution #1 (Click to Show/Hide) + +Code for `myApp.js` + +```javascript +/** 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: ["vodka", "air"]}); + + janeFonda.save(function(err, data) { + if (err) return console.error(err); + done(null, data) + }); +}; +``` +