1

Here is the problem, I'm trying to get the data I have saved from mongodb and show it in a table.

router.get('/page', function (req, res, next) {
     var VariableWithNoName = listAllResults();
     res.render('page', {title: 'page', dbresults: VariableWithNoName});  
});

And here is function that is supposed to retrieve the data from DB :

function listAllResults() {
  mongo.connect(process.env.MONGODB_URI, function(err, db) {
    assert.equal(null, err);
    var results = db.collection('mydatabase').find();
    var show = '<table>';
    results.each(function(err, result) {
      assert.equal(err, null);
      if (result != null) {
        show = show + '<tr><td>' + result['object']['BasicInfo[Name]'] + '</td></tr>';
      } else {
        callback();
      }

    });
    show = show + '</table>';
    return show;
  });
}

And here is my .hjs page :

<div id="ListOfResults">
    <p> Here there should be the list: </p>
    {{dbresults}}
</div>

Any idea what is wrong with it ?

Edited :

Data base structor :

{
"_id": {
    "$oid": "57626"
},
"object": {
    "BasicInfo[Name]": "Somethig"
}



2016-06-23T12:09:01.252374+00:00 heroku[router]: at=error code=H13 

desc="Connection closed without response" method=GET path="/page" host=test.herokuapp.com request_id=ee4b3c79-2884-4e1b-921f-772ae0a97b87 fwd="84.208.103.77" dyno=web.1 connect=0ms service=255ms status=503 bytes=0
2016-06-23T12:09:01.256277+00:00 app[web.1]: /app/node_modules/mongodb/lib/mongo_client.js:388
2016-06-23T12:09:01.256288+00:00 app[web.1]:               throw err
2016-06-23T12:09:01.256289+00:00 app[web.1]:               ^
2016-06-23T12:09:01.273399+00:00 app[web.1]: npm ERR! npm  v3.8.6
2016-06-23T12:09:01.275035+00:00 app[web.1]: npm ERR!     node ./bin/www
2016-06-23T12:09:01.275505+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2016-06-23T12:09:01.275805+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2016-06-23T12:09:01.279232+00:00 app[web.1]: 
Maziar
  • 81
  • 1
  • 12

1 Answers1

0

There are no results because the result from MongoDB in Node.js is asynchronous. Your listAllResults() function should take in a callback as argument so that you can use that callback in the router when you call the function.

Consider refactoring your code to accomodate the asynchronous function:

Async function

function listAllResults(callback) {
    mongo.connect(process.env.MONGODB_URI, function(err, db) {
        assert.equal(null, err);
        db.collection('mydatabase').find().toArray(function (err, results){
            if (err) throw err;         
            if (results != null) {
                var show = '<table>';
                results.each(function(err, result) {
                    show = show + '<tr><td>' 
                                + result['object']['BasicInfo[Name]'] 
                                + '</td></tr>';
                });
                show = show + '</table>';
                callback(show);
            }
            else {      
                // return empty table
                callback('<table></table>');
            }           
        });
    });
}

Route

router.get('/page', function (req, res, next) {
    listAllResults(function (VariableWithNoName){
        res.render('page', {title: 'page', dbresults: VariableWithNoName}); 
    });     
});

For detailed info on how to return a value from an asynchronous function, refer to these two questions:

Community
  • 1
  • 1
chridam
  • 100,957
  • 23
  • 236
  • 235