2018-10-25 20:29:56 +02:00
---
id: 587d7fb6367417b2b2512c09
2021-11-01 07:38:37 -07:00
title: Crea y guarda un registro de un modelo
2018-10-25 20:29:56 +02:00
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301536
2021-01-13 03:31:00 +01:00
dashedName: create-and-save-a-record-of-a-model
2018-10-25 20:29:56 +02:00
---
2020-11-27 19:02:05 +01:00
# --description--
2020-11-03 14:22:55 +00:00
2021-11-01 07:38:37 -07:00
En este desafío tendrás que crear y guardar un registro de un modelo.
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
# --instructions--
2020-11-03 14:22:55 +00:00
2021-11-01 07:38:37 -07:00
Dentro de la función `createAndSavePerson` , crea una instancia de documento usando el constructor del modelo `Person` que construiste antes. Pasa al constructor un objeto con los campos `name` , `age` y `favoriteFoods` . Sus tipos deben adaptarse a los del `personSchema` . Luego, llama al método `document.save()` en la instancia del documento devuelto. Pásale un callback usando la convención de Node. Este es un patrón común; todos los siguientes métodos CRUD toman una función de callback como este como el último argumento.
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
```
2020-11-27 19:02:05 +01:00
# --hints--
2018-10-25 20:29:56 +02:00
2021-11-01 07:38:37 -07:00
La creación y guardado de un elemento debe ser exitoso
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
```js
(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
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-10-25 20:29:56 +02:00
```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
```