0

Whenever a user follows some other user the userCollection is not updating unless server is restarted but in the mongo console the changes are shown even without restarting server.server.js is something like this-

var db=require('./database.js');
app.post('/addFollower',function(req,res){
    console.log(req.user);
    db.then(function(data){
           var userCollection=data.collection('users');
           userCollection.update({"username":req.user.username},{$addToSet:{"following":req.body.followWho}}, )
              userCollection.update({"username":req.body.followWho},{$addToSet:{"followers":req.user.username}})
                console.log("User followed")   ;
                console.log(req.user)
                  db=require('./database.js');    
                res.redirect('/posts')
})
})

I have console.log(req.user) before and after updation but it shows the same object.But in mongo console the req.user object is updated.Why is that? database.js

var mongodb=require('mongodb');
var MongoClient=mongodb.MongoClient;
var url='mongodb://localhost:27017/imageDatabase';

function getDb(){

    return MongoClient.connect(url).then(function (db) {
        // console.log(MongoClient.connect(url));
        return db;
    })
}

module.exports=getDb();
Community
  • 1
  • 1
Harshit Bisht
  • 65
  • 2
  • 10
  • Try also sharing the code in `database.js`. But wild guess so far—it might be the `db.then` triggering only when you connect/reconnect. Adding a `console.log` before the `var userCollection...` line should give you a clue on when they are called. – hyubs Aug 03 '17 at 20:01
  • what is the problem? – Harshit Bisht Aug 03 '17 at 20:12

0 Answers0