2018-10-25 20:29:56 +02:00
---
id: 587d7fb6367417b2b2512c09
title: Create and Save a Record of a Model
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301536
2018-10-25 20:29:56 +02:00
---
## Description
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'description' >
2020-11-03 14:22:55 +00:00
2019-03-09 06:49:19 -08:00
In this challenge you will have to create and save a record of a model.
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< / section >
## Instructions
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'instructions' >
2020-11-03 14:22:55 +00:00
Within the `createAndSavePerson` function, create a document instance using the `Person` model constructor you built before. Pass to the constructor an object having the fields `name` , `age` , and `favoriteFoods` . Their types must conform to the ones in the `personSchema` . Then, call the method `document.save()` 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.
2019-05-14 05:00:06 -07:00
```js
/* Example */
// ...
person.save(function(err, data) {
// ...do your stuff here...
2019-03-09 06:49:19 -08:00
});
2019-05-14 05:00:06 -07:00
```
2018-10-25 20:29:56 +02:00
< / section >
## Tests
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'tests' >
```yml
2018-10-27 12:53:05 +03:00
tests:
- text: Creating and saving a db item should succeed
2020-11-03 14:22:55 +00:00
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 20:29:56 +02:00
```
< / section >
## Challenge Seed
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'challengeSeed' >
< / section >
## Solution
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'solution' >
```js
2019-10-24 10:08:13 +05:30
/**
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 20:29:56 +02:00
```
2019-02-27 14:41:19 -05:00
2018-10-25 20:29:56 +02:00
< / section >