2019-05-06 07:31:26 -04:00
|
|
|
---
|
|
|
|
id: 587d7fb6367417b2b2512c06
|
|
|
|
title: Install and Set Up Mongoose
|
|
|
|
challengeType: 2
|
2020-08-16 04:50:44 +08:00
|
|
|
isHidden: false
|
|
|
|
localeTitle: 安装和设置 Mongoose
|
2019-05-06 07:31:26 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-16 04:50:44 +08:00
|
|
|
<section id='description'>
|
|
|
|
在 package.json 文件中添加 MongoDB 和 Mongoose 依赖,将 mLab 数据库的 URI 作为 MONGO_URI 变量存储在私有 .env 文件中。然后<code>require('mongoose')</code>,使用<code>mongoose.connect(<Your URI>)</code>命令来连接数据库。
|
|
|
|
|
|
|
|
```js
|
|
|
|
mongoose.connect(<Your URI>, { useNewUrlParser: true, useUnifiedTopology: true });
|
|
|
|
```
|
|
|
|
|
2019-05-06 07:31:26 -04:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
2020-08-16 04:50:44 +08:00
|
|
|
<section id='instructions'>
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-08-16 04:50:44 +08:00
|
|
|
- text: "在 package.json 文件中应该有 'mongodb' 依赖。"
|
2019-05-06 07:31:26 -04:00
|
|
|
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/file/package.json'').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, ''mongodb''); }, xhr => { throw new Error(xhr.responseText); })'
|
2020-08-16 04:50:44 +08:00
|
|
|
- text: "在 package.json 文件中应该有 'mongoose' 依赖。"
|
2019-05-06 07:31:26 -04:00
|
|
|
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/file/package.json'').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, ''mongoose''); }, xhr => { throw new Error(xhr.responseText); })'
|
2020-08-16 04:50:44 +08:00
|
|
|
- text: "'mongoose' 应该已经连接数据库。"
|
2019-05-06 07:31:26 -04:00
|
|
|
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/is-mongoose-ok'').then(data => {assert.isTrue(data.isMongooseOk, ''mongoose is not connected'')}, xhr => { throw new Error(xhr.responseText); })'
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-16 04:50:44 +08: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.
|
|
|
|
*/
|
2019-05-06 07:31:26 -04:00
|
|
|
```
|
2020-08-16 04:50:44 +08:00
|
|
|
|
2019-05-06 07:31:26 -04:00
|
|
|
</section>
|