freeCodeCamp/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of...

56 lines
1.7 KiB
Markdown
Raw Normal View History

2018-10-25 18:29:56 +00:00
---
id: 587d7fb6367417b2b2512c09
title: Create and Save a Record of a Model
challengeType: 2
forumTopicId: 301536
2018-10-25 18:29:56 +00:00
---
## Description
<section id='description'>
fix(curriculum) Replace <code> with <blockquote>for code sections (#35442) * fix: reformatted code and moved instructions * fix: replaced code with blockquotes * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: made a few recommended changes of text * fix: moved </blockquote> to new line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: removed extra space Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: added the word The before code tag section
2019-03-09 14:49:19 +00:00
In this challenge you will have to create and save a record of a model.
2018-10-25 18:29:56 +00:00
</section>
## Instructions
<section id='instructions'>
Create a document instance using the <code>Person</code> constructor you built before. Pass to the constructor an object having the fields <code>name</code>, <code>age</code>, and <code>favoriteFoods</code>. Their types must conform to the ones in the Person Schema. Then call the method <code>document.save()</code> on the returned document instance. Pass to it a callback using the Node convention. This is a common pattern, all the following CRUD methods take a callback function like this as the last argument.
```js
/* Example */
// ...
person.save(function(err, data) {
// ...do your stuff here...
fix(curriculum) Replace <code> with <blockquote>for code sections (#35442) * fix: reformatted code and moved instructions * fix: replaced code with blockquotes * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: made a few recommended changes of text * fix: moved </blockquote> to new line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: removed extra space Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: added the word The before code tag section
2019-03-09 14:49:19 +00:00
});
```
2018-10-25 18:29:56 +00:00
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: Creating and saving a db item should succeed
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/create-and-save-person'').then(data => { assert.isString(data.name, ''"item.name" should be a String''); assert.isNumber(data.age, ''28'', ''"item.age" should be a Number''); assert.isArray(data.favoriteFoods, ''"item.favoriteFoods" should be an Array''); assert.equal(data.__v, 0, ''The db item should be not previously edited''); }, xhr => { throw new Error(xhr.responseText); })'
2018-10-25 18:29:56 +00:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
</section>
## Solution
<section id='solution'>
```js
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
2018-10-25 18:29:56 +00:00
```
2018-10-25 18:29:56 +00:00
</section>