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){} */