Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

433
Views
MongoDB get data to display in HTML

I'm trying to display mongodb data in my html page. I've already managed to insert data in db but for some reason my "get" function does not work.

I'm using node.js with express framework and Angular for front-end and routing.

This is my "get" function to retreive data from MongoDB:

var mongo = require('mongodb');
var assert = require('assert');

var url = 'mongodb://localhost:27017/loodgieters';

router.get('/get-data', function(req, res, next) {
  var resultArray = [];
  mongo.connect(url, function(err, db){
    assert.equal(null, err);
    var cursor = db.collection('user-data').find();
    cursor.forEach(function(doc, err){
      assert.equal(null, err);
      resultArray.push(doc);
    }, function(){
      db.close();
      res.render('index', {items: resultArray});
    });
  });
});

And my "post" which works

router.post('/insert', function(req, res, next) {
  var item = {
    name: req.body.name,
    adress: req.body.adress,
    postal: req.body.postal,
    city: req.body.city,
    email: req.body.email,
    phone: req.body.phone,
    quotation: req.body.quotation,
    message: req.body.message
  };
  mongo.connect(url, function(err, db) {
    assert.equal(null, err);
    db.collection('user-data').insertOne(item, function(err, result){
      assert.equal(null, err);
      console.log('Item inserted');
      db.close();
    });
  });
  res.redirect('/contact');
});
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

i am not sure if this is the correct way to open and close mongo connection each time you are trying to query . if you want to go for another approach then use mongoose and follow something like this https://pastebin.com/g7aatzzj

about 4 years ago · Santiago Trujillo Report

0

I think that you have a mistake in your .find().forEach function callbacks. The error handling seems to be in the endCallback not the iteratorCallback.

According to the official doc, the correct way should be :

var mongo = require('mongodb');
var assert = require('assert');

var url = 'mongodb://localhost:27017/loodgieters';

router.get('/get-data', function(req, res, next) {
  var resultArray = [];
  mongo.connect(url, function(err, db){
    assert.equal(null, err);
    var cursor = db.collection('user-data').find({});
    cursor.forEach(function(doc){
      assert.notEqual(null, doc);
      resultArray.push(doc);
    }, function(err, doc){
      assert.equal(null, err);
      db.close();
      res.render('index', {items: resultArray});
    });
  });
});

This can also be found in their unit tests

    var cursor = collection.find({})
      .map(function(x) { return {a:1}; })
      .batchSize(5)
      .limit(10);
    cursor.forEach(function(doc) {
      test.equal(1, doc.a);
    }, function(err, doc) {
      test.equal(null, err);
      db.close();
      test.done();
    });

I think that you must have a error that is not passed to the first callback and not handled in the second one. So you do not see the error.

about 4 years ago · Santiago Trujillo Report

0

Try to insert an empty object to the find() function as following:

var cursor = db.collection('user-data').find({});

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!