简体   繁体   English

从mongodb检索数据

[英]Retrieving data from mongodb

Here is the problem, I'm trying to get the data I have saved from mongodb and show it in a table. 这是问题,我正在尝试从mongodb获取我保存的数据并将其显示在表格中。

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 : 这是应该从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 : 这是我的.hjs页面:

<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]: 

There are no results because the result from MongoDB in Node.js is asynchronous. 没有结果,因为Node.js中MongoDB的结果是异步的。 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. 您的listAllResults()函数应该将回调作为参数,以便在调用函数时可以在路由器中使用该回调。

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: 有关如何从异步函数返回值的详细信息,请参阅以下两个问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM