Docker:Mongo DB - Changes made via index.html was not updated in mongodb

I wrote a simple html code to run on node.js. Following has been done.

  1. Wrote a simple html code
  2. Dockerized mogodb and mogo-express.

Now when I made changes via html code its not updated in mongo db.

When i checked the logs its not communicating at all… Need some help to troubleshoot this one.

server.js

var express = require(‘express’);
var path = require(‘path’);
var fs = require(‘fs’);
var MongoClient = require(‘mongodb’).MongoClient;
var bodyParser = require(‘body-parser’);
var app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get( ‘/’, function( req, res ) {
res.sendFile( path.join( __dirname, “index.html”) );
});

app.get (‘/get-profile’, function( req, res ) {
if (err) throw err;

MongoClient.connect('mongodb://XXX:XXX@localhost:27017', function(err,client){
    if (err) throw err;

   var db = client.db('user-account');
   var query = { userid: 1 };
   db.createCollection('users').findOne(query, function(err, result) {
       if (err) throw err;
       client.close();
       response.send(result);
   });
});

});

app.post(‘/update-profile’, function(req, res) {
var userObj = req.body;
var response = res;

console.log('Connecting to the database...');

MongoClient.connect('mongodb://XXX:XXX@localhost:27017', function(err, client){
    if (err) throw err;

    var db = client.db('user-account');
    userObj ['userid'] = 1
    var query = { userid: 1 };
    var newValues = { $set: userObj };

    console.log('Successfully connected to the user-account database...');

    db.createCollection('users').updateOne(query, newValue, {upsert: true}, function(err, res) {
        if (err) throw err;
        console.log('User record inserted successful to the database...');
        client.close();
        response.send(userObj);
    });
});

});

app.get( ‘/Users/user/Downloads/Docker_Project/53876-25060.jpeg’, function( req, res ) {
var img = fs.readFileSync(‘profile-pic.jpg’ );
res.writeHead( 200, { ‘Content-Type’: ‘image/jpeg’ } );
res.end( img, ‘binary’ );
});

app.listen(3000, function() {
console.log(‘App listening on port 3000!’);
});

‘user-account’ is my db and ‘users’ is my collection.

html code

function editProfile() { document.querySelector('.container').style.display = 'none' document.querySelector('.container-edit').style.display = 'block'
        const name = document.querySelector('#name').textContent
        document.querySelector('#input-name').value = name

        const email = document.querySelector('#email').textContent
        document.querySelector('#input-email').value = email

        const interests = document.querySelector('#interests').textContent
        document.querySelector('#interests-edit').value = interests
    }

    function saveProfile() {
        document.querySelector('#name').textContent = document.querySelector('#input-name').value
        document.querySelector('#email').textContent = document.querySelector('#input-email').value
        document.querySelector('#interests').textContent = document.querySelector('#input-interests').value

        document.querySelector('.container').style.display = 'block'
        document.querySelector('.container-edit').style.display = 'none'
    }
</script>

User Profile

Name:

Mohamed Inshaf


Email:

inshaf@abc.com


Interests:

Learning Docker


<div calss = 'container-edit'>
    <h1 id="header" >Edit Profile</h1>
    <img sec="profile-pic.jpg">
    Name: <input id='input-name' type='text' />
    <hr />
    Email: <input id='input-email' type='emial'>
    <hr /> 
    Interests: <input id='input-interests' type='text'>
    <hr />
    <button class='button' onclick="saveProfile()">Save Profile</button>
</div>    

Hi @Mohamed_Inshaf

Please fix the formatting in the post, its difficult to read.

Are you running separate containers? localhost will not work for the docker uri, the name of the docker conatiner will be the hostname. Any container using the mongodb container will need to be on the same docker network.

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