---
id: 587d7fb6367417b2b2512c07
title: Create a Model
localeTitle: 创建一个模型
challengeType: 2
---
## Description
0
首先,我们需要一个Schema。每个模式都映射到MongoDB集合。它定义了该集合中文档的形状。 0
模式是模型的构建块。它们可以嵌套来创建复杂的模型,但在这种情况下,我们会保持简单。 0
模型允许您创建对象的实例,称为文档。 0
创建一个拥有此原型的人:
- Person Prototype -
--------------------
name : string [required]
age : number
favoriteFoods : array of strings (*)
0
使用mongoose基本模式类型。如果需要,还可以添加0
个字段,使用简单的验证器,如required或unique, 0
并设置默认值。请参阅mongoose文档 。
[C] RUD第一部分 - 创建0
注意:Glitch是一个真实的服务器,在真实服务器中,与db的交互发生在处理函数中。当某些事件发生时执行这些功能(例如某人在您的API上命中端点)。我们将在这些练习中遵循相同的方法。 done()函数是一个回调,告诉我们在完成插入,搜索,更新或删除等异步操作后我们可以继续。它遵循Node约定,应该在成功时调用done(null,data),或者在出错时调用(err)。 0
警告 - 与远程服务交互时,可能会发生错误!
/* Example */
var someFunc = function(done) {
//... do something (risky) ...
if(error) return done(error);
done(null, result);
};
## Instructions
## Tests
```yml
tests:
- text: 从mongoose模式创建实例应该会成功
testString: '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); })'
```
## Challenge Seed
## Solution
```js
// solution required
```