Encountering issues with connecting to MongoDB database and storing data in localhost:27017 using MongoClient.
Unable to display connection results in the console for both successful and failed connections.
var express = require('express');
var router = express.Router();
var mongoClient = require('mongodb').MongoClient
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/submit',function(req,res){
console.log(req.body)
mongoClient.connect('mongodb://localhost:27017',function(err,client){
if(err) {
console.log(err.message);
res.status(500).send('Internal Server Error');
}
else {
console.log('connection success')
client.db('Sample').collection('user').insertOne(req.body)
}
})
res.send('Yeah, got it !')
})
module.exports = router;
Attempting to connect to MongoDB and store data using MongoClient but facing challenges as data is not being stored in the database nor is the connection message appearing in the console.
Despite trying to output the connection result in the console, no messages such as 'connection success' or 'connection error' are displayed. The console remains empty.
Looking for a solution to resolve this issue while continuing to store data using the same method.
Additionally, seeking assistance on how to show output in the console indicating whether the connection was successful or if an error occurred.
Running mongodb version 7.0.4 and mongosh 2.0.2 on Windows with correct path settings, where mongo functions properly through the terminal. How can I troubleshoot and solve this problem?
...