My deployed application on Heroku was working fine until I encountered an issue with the connection to my MongoDB Atlas database. Despite my attempts to resolve it, the application keeps crashing. The Heroku logs show the following:
"2022-12-05T10:19:21.000000+00:00 app[api]: Build succeeded
2022-12-05T10:19:24.577606+00:00 heroku[web.1]: Starting process with command `npm start`
2022-12-05T10:19:26.144610+00:00 app[web.1]:
2022-12-05T10:19:26.144634+00:00 app[web.1]: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b07021f1f070e0f092b5a455b455b">[email protected]</a> start
2022-12-05T10:19:26.144634+00:00 app[web.1]: > node index.js
2022-12-05T10:19:26.144635+00:00 app[web.1]:
2022-12-05T10:19:26.369713+00:00 app[web.1]: Running on port: 36914
2022-12-05T10:19:26.725509+00:00 heroku[web.1]: State changed from starting to up
2022-12-05T10:19:56.365761+00:00 app[web.1]: Connected successfully to db server
2022-12-05T10:19:56.367567+00:00 app[web.1]: /app/node_modules/mongodb/lib/utils.js:698
2022-12-05T10:19:56.367568+00:00 app[web.1]: throw error;
2022-12-05T10:19:56.367569+00:00 app[web.1]: ^
2022-12-05T10:19:56.367569+00:00 app[web.1]:
2022-12-05T10:19:56.367571+00:00 app[web.1]: TypeError: Cannot read properties of undefined (reading 'db')
2022-12-05T10:19:56.367571+00:00 app[web.1]: at /app/dal.js:10:17
2022-12-05T10:19:56.367572+00:00 app[web.1]: at /app/node_modules/mongodb/lib/utils.js:695:9
2022-12-05T10:19:56.367572+00:00 app[web.1]: at /app/node_modules/mongodb/lib/mongo_client.js:285:23
2022-12-05T10:19:56.367573+00:00 app[web.1]: at connectCallback (/app/node_modules/mongodb/lib/op...
package.json
{
"name": "littledb",
"version": "1.0.0",
"description": "lowdb sample for 1.125",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "jest"
},
"author": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afcecdcac3efc2c6db81cacbda">[email protected]</a>",
"license": "MIT",
"dependencies": {
"cors": "^2.8.4",
"express": "^4.16.3",
"http-server": "^14.1.1",
"lowdb": "^1.0.0",
"mongodb": "^3.7.3",
"nodemon": "^2.0.20"
},
"devDependencies": {
"@types/mongodb": "^4.0.7"
}
}
dal.js
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
let db = null;
// connect to mongo
MongoClient.connect(url, {useUnifiedTopology: true}, function(err, client) {
db = client.db('baddestbank');
});
// create user account
function create(name, email, password){
return new Promise((resolve, reject) => {
const collection = db.collection('users');
})
}
// find user account
...
I have followed the instructions to resolve the issue with '@types/mongodb' in my VS Code but I'm still facing problems.
Despite having correct config vars on Heroku and MongoDB Atlas, my application is failing to sync data from the frontend to the MongoDB database. I have reviewed the IP address settings on Atlas as well.
The application functions correctly locally, but the deployment is causing the crashes. I have a deadline for my MIT project in 3 days, so I need to find a solution soon.
The expected behavior was for the app to connect successfully to the database and allow users to access their accounts, but currently, it is failing to push data and authenticate users.