2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7fb6367417b2b2512c07
|
2021-02-16 13:01:20 -08:00
|
|
|
|
title: 创建一个模型(Model)
|
2020-08-16 04:43:26 +05:30
|
|
|
|
challengeType: 2
|
2020-09-17 03:53:54 -07:00
|
|
|
|
forumTopicId: 301535
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: create-a-model
|
2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
**C** RUD 第一小节——CREATE
|
2020-09-17 03:53:54 -07:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
首先,我们需要一个 Schema, 每一个 Schema 都对应一个 MongoDB 的 collection, 并且在相应的 collection 里定义 documents 的“样子”。 Schema 用于组成模型(Model), 我们甚至可以通过嵌套 Schema 来创建复杂的模型。目前我们先从简。 我们可以根据模型创建实例,模型实例化后的对象称为 documents。
|
2020-09-17 03:53:54 -07:00
|
|
|
|
|
2021-05-12 21:25:58 +05:30
|
|
|
|
Replit 是一个真实的服务器,在其中,通过 handler 函数和数据库交互。 这些函数会在特定事件(比如有人调用了我们的服务器 API)发生时执行。 接下来的挑战题目即是以此为基础。 `done()` 是一个回调函数,它的作用是在一个异步操作(比如对数据库进行插入、查询、更新或删除)执行完成时,通知我们可以继续执行后续的其它代码。 这与 Node.js 中的处理方式十分类似,在 Node.js 中,我们会在(异步操作)成功时调用 `done(null, data)`,在失败时调用 `done(err)`。
|
2020-09-17 03:53:54 -07:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
注意:与远程服务器进行交互时,我们需要考虑到发生错误的可能!
|
2020-09-17 03:53:54 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
/* Example */
|
2020-09-17 03:53:54 -07:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
const someFunc = function(done) {
|
2020-09-17 03:53:54 -07:00
|
|
|
|
//... do something (risky) ...
|
2021-02-06 04:42:36 +00:00
|
|
|
|
if (error) return done(error);
|
2020-09-17 03:53:54 -07:00
|
|
|
|
done(null, result);
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
按下面的原型信息创建一个名为 `personSchema` 的 schema:
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-13 00:31:18 +08:00
|
|
|
|
```markup
|
2021-02-06 04:42:36 +00:00
|
|
|
|
- Person Prototype -
|
2021-01-13 00:31:18 +08:00
|
|
|
|
--------------------
|
|
|
|
|
name : string [required]
|
2021-02-06 04:42:36 +00:00
|
|
|
|
age : number
|
2020-09-17 03:53:54 -07:00
|
|
|
|
favoriteFoods : array of strings (*)
|
2021-01-13 00:31:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
采用 Mongoose 基础 schema 类型。 你如果还想添加更多的键,就请使用 required 或 unique 等简单的验证器(validators),并设置默认值。 详情请参考 [Mongoose 文档](http://mongoosejs.com/docs/guide.html)。
|
2021-01-13 00:31:18 +08:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
请从 `personSchema` 创建一个名为 `Person` 的 model。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
应当成功地通过 Mongoose schema 创建实例
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(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);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-05-06 07:31:26 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 00:31:18 +08:00
|
|
|
|
```js
|
|
|
|
|
/**
|
2021-01-13 03:31:00 +01:00
|
|
|
|
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.
|
2021-01-13 00:31:18 +08:00
|
|
|
|
*/
|
|
|
|
|
```
|