freeCodeCamp/curriculum/challenges/espanol/05-back-end-development-and.../mongodb-and-mongoose/create-a-model.md

89 lines
3.0 KiB
Markdown
Raw Normal View History

2018-10-25 18:29:56 +00:00
---
id: 587d7fb6367417b2b2512c07
title: Crea un modelo
2018-10-25 18:29:56 +00:00
challengeType: 2
forumTopicId: 301535
dashedName: create-a-model
2018-10-25 18:29:56 +00:00
---
# --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
**C**RUD Parte 1: CREATE (Crear)
En primer lugar, necesitamos un esquema. Cada esquema asigna a una colección de MongoDB. Define la forma de los documentos dentro de esa colección. Los esquemas son bloques de construcción para los modelos. Pueden ser anidados para crear modelos complejos, pero en este caso las cosas serán sencillas. Un modelo te permite crear instancias de tus objetos, llamados documentos.
Replit es un servidor real, y en servidores reales las interacciones con la base de datos ocurren en funciones gestoras (handler functions). Estas funciones se ejecutan cuando ocurre algún evento (por ejemplo, alguien golpea un endpoint en tu API). Seguiremos el mismo enfoque en estos ejercicios. La función `done()` es un callback que nos dice que podemos proceder después de completar una operación asincrónica como insertar, buscar, actualizar o eliminar. Sigue la convención de Node, y debe ser llamado como `done(null, data)` en caso de éxito, o `done(err)` en caso de error.
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
Advertencia: ¡Al interactuar con servicios remotos, pueden ocurrir errores!
```js
/* Example */
const someFunc = function(done) {
//... do something (risky) ...
if (error) return done(error);
done(null, result);
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
};
```
# --instructions--
Crea un esquema de persona llamado `personSchema` con este prototipo:
```markup
- Person Prototype -
--------------------
name : string [required]
age : number
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
favoriteFoods : array of strings (*)
```
Usa los tipos básicos de esquemas de Mongoose. Si quieres también puedes añadir más campos, utilizar validadores sencillos como required o unique, y establecer valores por defecto. Consulta la [documentación de Mongoose](http://mongoosejs.com/docs/guide.html).
Ahora, crea un modelo llamado `Person` del `personSchema`.
2018-10-25 18:29:56 +00:00
# --hints--
2018-10-25 18:29:56 +00:00
La creación de una instancia a partir de un esquema mongoose debe ser exitosa
```js
(getUserInput) =>
$.post(getUserInput('url') + '/_api/mongoose-model', {
name: 'Mike',
age: 28,
favoriteFoods: ['pizza', 'cheese']
}).then(
(data) => {
assert.equal(data.name, 'Mike', '"model.name" is not what expected');
assert.equal(data.age, '28', '"model.age" is not what expected');
assert.isArray(
data.favoriteFoods,
'"model.favoriteFoods" is not an Array'
);
assert.include(
data.favoriteFoods,
'pizza',
'"model.favoriteFoods" does not include the expected items'
);
assert.include(
data.favoriteFoods,
'cheese',
'"model.favoriteFoods" does not include the expected items'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2018-10-25 18:29:56 +00:00
```
# --solutions--
2018-10-25 18:29:56 +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.
*/
2018-10-25 18:29:56 +00:00
```