Issue getting Mongo connection to pass

I’m using replit. I have created an account , cluster and db on mongo atlas. I can connect to the account/db using Mongo DB compass browser from my mac (i.e. not using replit). I have set up replit with the same access/auth credentials string but when I try to use connection.readystate and wait for it to become a 1 (which means connected) it never does. When I run the freecodecamp test it complains that I’m not connected to a database and that I don’t have the mongo and mongoose stuff in my package file but replit already has those things in the package-lock.json file although they are newer versions (note: I had to remove links because I’m a new user and would not let me post if more than 2 links are embedded).
Here is what is in the replit locked package file already:“mongodb”: {
“version”: “4.3.1”,
“resolved”:
“integrity”: “sha512-sNa8APSIk+r4x31ZwctKjuPSaeKuvUeNb/fu/3B6dRM02HpEgig7hTHM8A/PJQTlxuC/KFWlDlQjhsk/S43tBg==”,
“requires”: {
“bson”: “^4.6.1”,
“denque”: “^2.0.1”,
“mongodb-connection-string-url”: “^2.4.1”,
“saslprep”: “^1.0.3”,
“socks”: “^2.6.1”
}
},
“mongodb-connection-string-url”: {
“version”: “2.4.2”,
“resolved”:
“integrity”: “sha512-mZUXF6nUzRWk5J3h41MsPv13ukWlH4jOMSk6astVeoZ1EbdTJyF5I3wxKkvqBAOoVtzLgyEYUvDjrGdcPlKjAw==”,
“requires”: {
@types/whatwg-url”: “^8.2.1”,
“whatwg-url”: “^11.0.0”
}
},
“mongoose”: {
“version”: “6.2.1”,
“resolved”:
“integrity”: “sha512-VxY1wvlc4uBQKyKNVDoEkTU3/ayFOD//qVXYP+sFyvTRbAj9/M53UWTERd84pWogs2TqAC6DTvZbxCs2LoOd3Q==”,
“requires”: {
“bson”: “^4.2.2”,
“kareem”: “2.3.3”,
“mongodb”: “4.3.1”,
“mpath”: “0.8.4”,
“mquery”: “4.0.2”,
“ms”: “2.1.2”,
“sift”: “13.5.2”
}
},

If I console log my URI it is correct and the same as I can connect with using Compass. It is:
mongodb+srv://cavasian:***@cluster0.udg5j.mongodb.net/dbone?retryWrites=true&w=majority
where I have replaced my password with *** in this posting.
When I run my app.js in replit here is what the console shows:

fcc-mongo-mongoose-challenges@0.0.1 start /home/runner/boilerplate-mongomongoose
node server.js

ready state = 2
Your app is listening on port 3000

My app.js code is below. (note if I enable the while loop that is commented out then execution never leaves the while loop).

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

async function myFunc(){
try {
await mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
} catch (error){
console.log("Error: — " + error);
}
}

myFunc();

console.log('ready state = ’ + mongoose.connection.readyState);

/* while (mongoose.connection.readyState != 1){} */

I had a similar issue, I followed the advice given here and it worked - simply put create a new user with a different username to the one you used to set up your mongoDB

I just tried what double-dash-dino suggested and same result. Can make connection fine with mongo Compass but don’t think I’m getting successful connection with replit (auth string above modified for the new user I added). I don’t think I get a successful connection because mongoose.connection readyState never equals 1 which would indicate connected. And still don’t know what the freecodecamp challenge complains about the package inclusions that are present in the locked json package on replit. Clearly I know how to make a connection since compass works fine and I can’t get past this exercise.

Ok, I got this challenge to pass finally. One thing I found out is that using the mongoose.connection.readyState to determine if I was connected was misleading me to think I was not connected when I actually was. No idea why that was returning a ‘2’ meaning connected even though I was successfully connected. Not sure why the freecodecamp was not recognizing that I was connected either but when I skipped ahead to exercises where I stored something and then came back to this it then recognized the connection and passed. So who knows. I used Compass to see when stuff was created and stored.

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