MongoDB and Mongoose - Install and Set Up Mongoose

Tell us what’s happening:
Everytime I run my code I get an error

Your code so far
require(‘dotenv’).config();

const dotenv = require(‘dotenv’);
dotenv.config({path:‘sample.env’});

let mongoose = require(‘mongoose’)
let Person;

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

Your browser information:
boilerplate-mongomongoose-2.tffree.repl.co
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: MongoDB and Mongoose - Install and Set Up Mongoose

Link to the challenge:

what error do you get?!

You can’t use an .env file in replit. You should store your environment variables in your Secrets tab instead.

Also the sample.env file shouldn’t be used for accessing environment variables anyhow. The purpose of that file is to show other developers what variables you are using, without giving away sensitive information such as usernames/passwords etc. The variables and their values would still be stored in the .env file.

This is my error:
// running tests
“mongoose version ^5.11.15” dependency should be in package.json
“mongoose” should be connected to a database
// tests completed
// console output
[Error]
[Error]

How do I fix the error?

Firstly, it’s much more helpful if you can share a link to your code rather than your live project link: https://replit.com/@tffree/boilerplate-mongomongoose-2

Your repl won’t run because of the following lines of code:

equire(‘dotenv’).config();

const dotenv = require(‘dotenv’);
dotenv.config({path:‘sample.env’});

let mongoose = require(‘mongoose’)
let Person;

You’ve lost the ‘r’ from ‘require’.
Also, all of those quote marks in your repl are smart quotes, which throw an error. I guess it’s because you’ve pasted in this code from somewhere on the forum perhaps? You need to retype all of those single quotes by hand to fix this.

You don’t actually need the two middle lines of code either, as you’re working from your Secrets tab (rather than an .env file).

https://boilerplate-mongomongoose.tffree.repl.co

require(“dotenv”);

let mongoose = require(“mongoose”)
let Person;

const mongoose = require(“mongoose”);
mongoose.connect(process.env.MONGO_URI);

Now what did I do wrong?

  • its missing “config” method chained on it!!

You’ve switched to a different repl now:
https://replit.com/@tffree/boilerplate-mongomongoose

This repl has different code than the one you originally posted:

require("dotenv");

let mongoose = require("mongoose")
let Person;

const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI);

As @bappyasif pointed out, your code now doesn’t have the config method chained to dotenv.

Also, is there a reason why you declare mongoose twice?
In your mongoose connection you’ve also lost the configuration object which parses the url.

Finally, you now have your username and password on display publicly in your sample.env file. You should delete this file and store your MONGO_URI only in your Secrets tab, where no-one but you can access it.

If you follow the challenge instructions carefully, you should be able to set this up properly.

require(“dotenv”);

dotenv.config({path:“sample.env”});

let mongoose = require(“mongoose”)
let Person;

const mongoose = require(“mongoose”);
mongoose.connect(process.env.MONGO_URI);

const createAndSavePerson = (done) => {
done(null /, data/);
};

const createManyPeople = (arrayOfPeople, done) => {
done(null /, data/);
};

const findPeopleByName = (personName, done) => {
done(null /, data/);
};

const findOneByFood = (food, done) => {
done(null /, data/);
};

const findPersonById = (personId, done) => {
done(null /, data/);
};

const findEditThenSave = (personId, done) => {
const foodToAdd = “hamburger”;

done(null /, data/);
};

const findAndUpdate = (personName, done) => {
const ageToSet = 20;

done(null /, data/);
};

const removeById = (personId, done) => {
done(null /, data/);
};

const removeManyPeople = (done) => {
const nameToRemove = “Mary”;

done(null /, data/);
};

const queryChain = (done) => {
const foodToSearch = “burrito”;

done(null /, data/);
};
now what did i do wrong?

  • You do not need dotenv on Replit.

  • You still have mongoose twice.

// require("dotenv");

// dotenv.config({path:"sample.env"});

// let mongoose = require("mongoose")
let Person;

const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI);

Make sure you added the environment variable to the secrets

require(“dotenv”);

dotenv.config({path:“sample.env”});

let mongoose = require(“mongoose”)
let Person;

mongoose.connect(process.env.MONGO_URI), { useNewUrlParser: true, useUnifiedTopology: true};

require(“dotenv”);

dotenv.config({path:“sample.env”});

let mongoose = require(“mongoose”)
let Person;

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true});

Now what am i doing wrong?

As I said, just remove the dotenv code, you do not need it.


If you want to know how to use it look at the docs

require('dotenv').config({ path: '/custom/path/to/.env' })

Thank you, I don’t need anymore help I just completed the exercise

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.