0

In my second connection to mongo db (where I handle the POST data), assert throws an error. I am new to the topic and maybe I forgot to consider something on the server side? But restart the mongo server brought also no solution.

Basically, I want to insert the user input into the collection. Don´t know where the error is, anybody?

var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var app = express();
var server = require('http').createServer(app);
var userName;
var userAge;
var user;
var url = "mongodb://localhost:27017/myAppDB";
var multer  = require('multer');
var upload = multer();

app.set("view engine", "ejs");
app.use(express.static(__dirname + '/public'));
app.use(session({
    secret: "schhh",
    cookie: {maxAge: 86400000},
}));


server.listen(3000, function () {
console.log('Server listens on port 3000');
});


//method for console.logging the whole collection
var findUser = function(db, callback) {
   var entry = db.collection('user').find( );
   entry.each(function(err, doc) {
   assert.equal(null, err);
   if (doc != null) {
         console.log(doc);
  } else {
     callback();
     console.log("Exit DB");
  }
});
}; 


//first connection to mongo
MongoClient.connect(url, function(err, db) {
      assert.equal(null, err);
      if(err) { 
        return console.log(err);
      } else {
        console.log("Connected to DB");
      }

          var collection = db.collection('user');

          var user1 = {username: 'Max Mustermann',
                        position:'Student',
                        eMail:'max@mustermann.de'};
          var user2 = {username: 'Ute Mustermann',
                        position:'Student',
                        eMail:'max@mustermann.de'};
          var alleUser = [user1, user2];

          collection.insert(alleUser);

          findUser(db, function() {
              db.close();
          });
});



app.get('/', function (req, res, next) {
    res.render('index.ejs');
    var visit = JSON.stringify(req.session);
    req.session.last_visit = Date.now();
    console.log(„Last visit: " + visit);

});



app.post('/', upload.array(), function (req, res, next) {

    var newUser = {
        username: req.body.vorname,
        position: req.body.pos,
        eMail: req.body.mail
    };

MongoClient.connect(url, function(err, db) {
    assert.equal(null, err); 
    db.collection('user').insert(newUser, function(err, doc){ //Assert returns  true here
    assert(null, err);
    });
});

});
Community
  • 1
  • 1
  • _The first parameter will contain the Error object if an error occured, or null otherwise._ - Try to log the error to see what it's about. I think err.err will help you - http://stackoverflow.com/questions/20597773/what-does-an-example-mongodb-error-look-like-on-the-nodejs-native-driver – dmg_ Jun 30 '16 at 14:26
  • When I am logging the error, I get: TypeError: Cannot read property 'error' of null, what does it mean? – user3612671 Jun 30 '16 at 14:36
  • Ah yes it asserts true you commented for null, meaning there is no error there, I guess. Which error is thrown? – dmg_ Jun 30 '16 at 14:46
  • It depends on how I am logging the error. But it is always 'null', I guess my insert doesn´t work... – user3612671 Jun 30 '16 at 14:48
  • 1
    Null should indicate there is no error, so I guess there is none – dmg_ Jun 30 '16 at 14:54
  • 1
    Ahhh, now I found it: `assert(null, err)` should be `assert.equal(null, err)` and this is why I get a true for null. After your hint, I saw my the names of my tags were different spelled in my html file as in my `app.post`. Thank you. – user3612671 Jun 30 '16 at 15:05
  • I'm glad it helped in some way) – dmg_ Jun 30 '16 at 15:09

0 Answers0