2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7fb6367417b2b2512c06
|
2021-03-04 09:49:46 -08:00
|
|
|
|
title: 安装和设置 Mongoose
|
2019-05-06 07:31:26 -04:00
|
|
|
|
challengeType: 2
|
2020-09-17 03:53:54 -07:00
|
|
|
|
forumTopicId: 301540
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: install-and-set-up-mongoose
|
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
|
|
|
|
你可以采用下面的任意一种编写代码的方式来完成这些挑战:
|
2021-03-04 09:49:46 -08:00
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
- 克隆 [这个 GitHub 仓库](https://github.com/freeCodeCamp/boilerplate-mongomongoose/) 并在本地完成项目。
|
2021-05-12 21:25:58 +05:30
|
|
|
|
- 使用[我们的 Replit 上的初始化项目](https://replit.com/github/freeCodeCamp/boilerplate-mongomongoose)来完成项目。
|
2021-09-18 11:22:53 -07:00
|
|
|
|
- 使用你选择的网站生成器来完成项目。 需要包含我们 GitHub 仓库的所有文件。
|
2021-03-04 09:49:46 -08:00
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
完成本项目后,请将一个正常运行的 demo(项目演示)托管在可以公开访问的平台。 然后在 `Solution Link` 字段中提交它的 URL。
|
2021-03-04 09:49:46 -08:00
|
|
|
|
|
|
|
|
|
在这个挑战中,你将建立一个 MongoDB Atlas 数据库并导入连接到它所需的软件包。
|
|
|
|
|
|
|
|
|
|
按照<a href='https://www.freecodecamp.org/news/get-started-with-mongodb-atlas/' rel='noopener noreferrer' target='_blank'>这篇教程</a>在 MongoDB Atlas 创建一个托管数据库。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
将 `mongodb@~3.6.0` 和 `mongoose@~5.4.0` 添加到项目的 `package.json` 中。 然后,在 `myApp.js` 文件中请求 `mongoose`。 创建一个 `.env` 文件,给它添加一个 `MONGO_URI` 变量。 变量的值为你的 MongoDB Atlas 数据库 URI。 应用单引号或双引号包裹 URI。请记住,环境变量 `=` 两边不能有空格。 例如,`MONGO_URI='VALUE'`。 完成后,使用下面的代码来连接数据库。
|
2020-09-17 03:53:54 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
mongoose.connect(<Your URI>, { useNewUrlParser: true, useUnifiedTopology: true });
|
2020-09-17 03:53:54 -07:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-03-04 09:49:46 -08:00
|
|
|
|
“mongodb” 应在 package.json 中作为依赖项定义。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(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);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-05-06 07:31:26 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-04 09:49:46 -08:00
|
|
|
|
“mongoose” 应在 package.json 中作为依赖项定义。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(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);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
```
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-03-04 09:49:46 -08:00
|
|
|
|
应使用 “mongoose” 连接数据库。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/is-mongoose-ok').then(
|
|
|
|
|
(data) => {
|
|
|
|
|
assert.isTrue(data.isMongooseOk, 'mongoose is not connected');
|
|
|
|
|
},
|
|
|
|
|
(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 03:31:00 +01: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.
|
|
|
|
|
*/
|
|
|
|
|
```
|