2021-06-15 00:49:18 -07:00
---
id: 587d7fb6367417b2b2512c06
2021-07-09 21:23:54 -07:00
title: Installare e configurare Mongoose
2021-06-15 00:49:18 -07:00
challengeType: 2
forumTopicId: 301540
dashedName: install-and-set-up-mongoose
---
# --description--
2021-07-09 21:23:54 -07:00
Lavorare su queste sfide ti porterà a scrivere il tuo codice utilizzando uno dei seguenti metodi:
2021-06-15 00:49:18 -07:00
2021-07-09 21:23:54 -07:00
- Clonare [questo repository GitHub ](https://github.com/freeCodeCamp/boilerplate-mongomongoose/ ) e completare queste sfide localmente.
2021-07-19 16:05:37 +05:30
- Usare [la nostra bozza di progetto su Replit ](https://replit.com/github/freeCodeCamp/boilerplate-mongomongoose ) per completare queste sfide.
2021-08-25 09:12:11 -07:00
- Usare un costruttore di siti di tua scelta per completare il progetto. Assicurati di incorporare tutti i file del nostro repository GitHub.
2021-06-15 00:49:18 -07:00
2021-08-25 09:12:11 -07:00
Quando hai finito, assicurati che una demo funzionante del tuo progetto sia ospitata in qualche percorso pubblico. Quindi invia l'URL nel campo `Solution Link` .
2021-06-15 00:49:18 -07:00
2021-07-09 21:23:54 -07:00
In questa sfida, imposterai un database MongoDB Atlas e importerai i pacchetti necessari per connetterti ad esso.
2021-06-15 00:49:18 -07:00
2021-07-09 21:23:54 -07:00
Segui < a href = 'https://www.freecodecamp.org/news/get-started-with-mongodb-atlas/' rel = 'noopener noreferrer' target = '_blank' > questo tutorial< / a > per impostare un database ospitato su MongoDB Atlas.
2021-06-15 00:49:18 -07:00
# --instructions--
2021-08-31 09:47:25 -07:00
Aggiungi `mongodb@~3.6.0` e `mongoose@~5.4.0` al `package.json` del progetto. Poi, richiedi mongoose come `mongoose` in `myApp.js` . Crea un file `.env` e aggiungi una variabile `MONGO_URI` ad esso. Il suo valore dovrebbe essere l'URI del database MongoDB Atlas. Assicurati di racchiudere l'URI tra virgolette singole o doppie, e ricorda che non puoi usare spazi attorno al segno `=` nelle variabili d'ambiente. Ad esempio, `MONGO_URI='VALUE'` . Quando hai finito, connettiti al database usando la seguente sintassi:
2021-06-15 00:49:18 -07:00
```js
mongoose.connect(< Your URI > , { useNewUrlParser: true, useUnifiedTopology: true });
```
# --hints--
2021-07-09 21:23:54 -07:00
la dipendenza "mongodb" dovrebbe essere specificata in package.json
2021-06-15 00:49:18 -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);
}
);
```
2021-07-09 21:23:54 -07:00
la dipendenza "mongoose" dovrebbe essere specificata in package.json
2021-06-15 00:49:18 -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);
}
);
```
2021-07-09 21:23:54 -07:00
"mongoose" dovrebbe essere connesso a un database
2021-06-15 00:49:18 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/is-mongoose-ok').then(
(data) => {
assert.isTrue(data.isMongooseOk, 'mongoose is not connected');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
# --solutions--
```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.
*/
```