MongoDB and Mongoose - Use model.findOne() to Return a Single Matching Document from Your Database

Tell us what’s happening:
I can’t connect to my database anymore. I was able to get this far in the MongoDB course, but am now getting this error when I start my app:

Your app is listening on port 3000
/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:847
const serverSelectionError = new ServerSelectionError();
^

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. Make sure your current IP address is on your Atlas cluster’s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:847:32)
at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:351:10
at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise ()
at promiseOrCallback (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:350:20)
at Object. (/home/runner/boilerplate-mongomongoose/myApp.js:4:10)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/home/runner/boilerplate-mongomongoose/server.js:67:16)
at Module._compile (node:internal/modules/cjs/loader:1105:14) {
reason: TopologyDescription {
type: ‘ReplicaSetNoPrimary’,
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(3) {
ac-gx2tdzf-shard-00-00.6pcdo3v.mongodb.net:27017’ => ServerDescription {
address: ‘ac-gx2tdzf-shard-00-00.6pcdo3v.mongodb.net:27017’,
error: Error: getaddrinfo ENOTFOUND ac-gx2tdzf-shard-00-00.6pcdo3v.mongodb.net
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
name: ‘MongoNetworkError’
},
roundTripTime: -1,
lastUpdateTime: 56847883,
lastWriteDate: null,
opTime: null,
type: ‘Unknown’,
topologyVersion: undefined,
minWireVersion: 0,
maxWireVersion: 0,
hosts: ,
passives: ,
arbiters: ,
tags:
},
ac-gx2tdzf-shard-00-01.6pcdo3v.mongodb.net:27017’ => ServerDescription {
address: ‘ac-gx2tdzf-shard-00-01.6pcdo3v.mongodb.net:27017’,
error: Error: getaddrinfo ENOTFOUND ac-gx2tdzf-shard-00-01.6pcdo3v.mongodb.net
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
name: ‘MongoNetworkError’
},
roundTripTime: -1,
lastUpdateTime: 56847857,
lastWriteDate: null,
opTime: null,
type: ‘Unknown’,
topologyVersion: undefined,
minWireVersion: 0,
maxWireVersion: 0,
hosts: ,
passives: ,
arbiters: ,
tags:
},
ac-gx2tdzf-shard-00-02.6pcdo3v.mongodb.net:27017’ => ServerDescription {
address: ‘ac-gx2tdzf-shard-00-02.6pcdo3v.mongodb.net:27017’,
error: Error: getaddrinfo ENOTFOUND ac-gx2tdzf-shard-00-02.6pcdo3v.mongodb.net
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
name: ‘MongoNetworkError’
},
roundTripTime: -1,
lastUpdateTime: 56847976,
lastWriteDate: null,
opTime: null,
type: ‘Unknown’,
topologyVersion: undefined,
minWireVersion: 0,
maxWireVersion: 0,
hosts: ,
passives: ,
arbiters: ,
tags:
}
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
exit status 1

I have my DB settings to allow all connections so my IP is certainly whitelisted, and the challenges were working fine before and aren’t now. Any suggestions are appreciated!

Your code so far
My code is as follows:

require(‘dotenv’).config();
const mongoose = require(“mongoose”);

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

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

const Person = mongoose.model(“Person”, personSchema);

const createAndSavePerson = (done) => {
let john = new Person({name: “John”, age: 27, favoriteFoods: [“Banana”]});
john.save(
(err, data) => {
if (err) return console.error(err);
done(null, data);
});
};

const arrayOfPeople = [
{name: “Austin”, age: 29, favoriteFoods: [‘Empanadas’]},
{name: ‘Calvin’, age: 19, favoriteFoods: [‘Mtn. Dew’]},
{name: ‘Carter’, age: 21, favoriteFoods: [‘Sandwiches’]}
];

const createManyPeople = (arrayOfPeople, done) => {
Person.create(arrayOfPeople, (err, people) => {
if (err) return console.log(err);
done(null, people);
});
};

const findPeopleByName = (personName, done) => {
Person.find({name: personName}, (err, foundPerson) => {
if (err) return console.log(err);
done(null, foundPerson);
});
};

const findOneByFood = (food, done) => {
Person.findOne({favoriteFoods: food}, (err, foundPerson) => {
if (err) return console.log(err);
done(null, foundPerson);
});
};

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;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: MongoDB and Mongoose - Use model.findOne() to Return a Single Matching Document from Your Database

Link to the challenge:

Please provide a link to your project code, so we can test it.

Here’s my Replit: boilerplate-mongomongoose - Replit

When I supply my own MONGO_URI, it passes the challenge with no issues. Based on the error message, you may have not configured the Network Access to allow any ip address. It could also be your are using the wrong database username and password.

As said, it sounds like an IP whitelist issue.

If you have a dynamic IP (which most people do) whitelisting your current IP may cause it to fail later if your IP changes. Whitelist 0.0.0.0/0 if you did not do so.

Adding the CIDR 0.0.0.0/0 allows access from anywhere.

Okay, so it literally just started working without me changing anything. It must have been some issue on MongoDB’s side. Thanks for your input!