Please Help! Create and Save a Record of a Model Not Passing With Replit

Been trying to get past this challenge for over 6 hours using replit. Whenever I run my code in an attempt to establish connection with my MongoDB Atlas cluster, I get this annoying error:

MongooseServerSelectionError: Could not connect to any servers in your Mongodb Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted...(bla bla bla)
.
.
{
reason: TopologyDescription {
.....
.....
  }
}

Meanwhile I already allowed access from anywhere on my mongodb cluster Newtwork Access.

Also when I submit my solution link I get the error below logged to my repl console:

MongooseError...

And here is the code in my myApp.js file:

require('dotenv').config();

var mongoose = require('mongoose');

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }).then(() => console.log("Database connected"))
.catch(err => console.error(err));

const personSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    favoriteFoods: [String]
  });

let Person = mongoose.model('Person', personSchema);

const createAndSavePerson = (done) => {
  const person1 = new Person({
    name: "Bankole Ayeni", 
    age: 22,
    favoriteFoods: ["eba with gbegiri", "amala with ewedu"]
  });
  
  person1.save((err, data) => {
    return err ? done(err) : 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*/);
};

/** **Well Done !!**
/* You completed these challenges, let's go celebrate !
 */

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;

My MONGO_URI in the Secrets(Environment variables) looks like this:

ā€œmongodb://niranad:*****@solvexdb1.pivcw.mongodb.net/fccdb?retryWrites=true&w=majorityā€

Here is a link to my repl:

boilerplate-mongomongoose-1 - Replit

I wonder if this challenge ever pass with replit?
Please help!
Thanks.

1 Like

Hello there,

As this is a Mongoose connection error, I do not see anything that can be done about your code. That is, my best guess would be to play around with your Atlas account settings.

Hope you can find a solution

1 Like

As this is my first time using mongodb, I carefully followed the freecodecampā€™s guide on setting up my mongodb atlas account. And I believe the problem is not with the Network Access setting as connections can be made from any IP address.

However I noticed that when I tried connecting from MongoCompass I couldnā€™t connect with a non-srv string. Only the ā€˜mongodb+srvā€™ string worked.
And when I tried using the srv string on my repl, I get an error:

TypeError: Cannot read property ā€˜splitā€™ of null at parseSrvConnectionStringā€¦at connectā€¦at maybePromiseā€¦at MongoClient.connectā€¦atā€¦

Could it be a mongoose (^5.9.5) incompatibility issue with mongodb cluster version 4.4.6?

1 Like

Just tried another version of the connection string for node.js 2.2.12 or later from my atlas account and it worked! My app now connects with the database but I still canā€™t pass the challenge.

Any help with that would be appreciated.

1 Like

Thanks for sharing very helpful content, i am new to programming!

1 Like

Hello,

After much hassle and headaches, I figured out the problem is with the process.env.MONGO_URI reference in the mongoose.connect method. I had to paste the connection string directly into the connect method for the challenge to pass. Maybe thereā€™s some sort of secret .env file lurking around somewhere with other uriā€™s except the one in my Secrets tab.

In case anyone has this kind of annoying issue in the future, they should just forego the use of the Secrets variable (as you canā€™t create a .env file on repl) accessed with the process.env reference and copy the damn string directly.

Appreciate your support. Thanks.

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