2019-05-06 11:31:26 +00:00
---
id: 587d7fb6367417b2b2512c09
2021-02-06 04:42:36 +00:00
title: Create and Save a Record of a Model
2019-05-06 11:31:26 +00:00
challengeType: 2
2020-09-17 10:53:54 +00:00
forumTopicId: 301536
2021-01-13 02:31:00 +00:00
dashedName: create-and-save-a-record-of-a-model
2019-05-06 11:31:26 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
In this challenge you will have to create and save a record of a model.
2019-05-06 11:31:26 +00:00
2020-12-16 07:37:30 +00:00
# --instructions--
2021-02-06 04:42:36 +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.
2020-09-17 10:53:54 +00:00
```js
2021-02-06 04:42:36 +00:00
/* Example */
2020-09-17 10:53:54 +00:00
// ...
person.save(function(err, data) {
// ...do your stuff here...
});
```
2019-05-06 11:31:26 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2019-05-06 11:31:26 +00:00
2021-02-06 04:42:36 +00:00
Creating and saving a db item should succeed
2019-05-06 11:31:26 +00:00
```js
2020-12-16 07:37:30 +00:00
(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);
}
);
2019-05-06 11:31:26 +00:00
```
2020-08-13 15:24:35 +00:00
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```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.
*/
```