2017-02-24 65 views
0

我为这个提交的可怕标题appologise,但我一直在撕掉我的头发至少8小时,现在试图解决这个问题。查询Node.JS中的MongoDB数据库时发现空结果

我最初采取从SOVA对快递结果显示MongoDB的文档一些指导翡翠Express displaying mongodb documents in Jade

然而,每当我尝试做失败的查询。

我的index.js代码是这样的。

app.get('/test', function (req, res) { 
var MongoClient = require('mongodb').MongoClient 
var url = 'mongodb://localhost/quizmaster'; 

var results_from_mongo = []; 

MongoClient.connect(url, function (err, db) { 
    var str = db.collection('qmquestions').find(); 
    str.each(function (err, doc) { 
      //console.log(doc); 
      results_from_mongo.push(doc); 
      console.log(results_from_mongo) //Push result onto results_array 
    }); 

    //now we have a results array filled like this: 
    // results_from_mongo = ["some string", "some string", "some string"] 
    //so let's pass them to the jade file to render them. 
    res.render('test', {results_from_mongo : results_from_mongo }); 
    }); 
}); 

我test.jade代码是这样的

block content 
h1= title 
h2= "results from mongo:" 
select 
    each results_from_mongos, i in results_from_mongo 
    option(value=i) #{results_from_mongos} 

我甚至曾经尝试这样做(test.jade)的哈巴狗变化

 table 
      thead 
       tr 
        th Question name 
        th Question 
        th Answer 
      tbody 
       each results_from_mongo, i in results 
        tr 
         td= results_from_mongo.questionTitle 
         td= results_from_mongo.question 
         td= results_from_mongo.answer 

从该db.collections.find直接MongoDB的结果是

{ "_id" : ObjectId("58af574c4fef02081c32da2f"), "question" : { "questionTitle" : "Test", "question" : "Test", "answer" : "Test" } } 

我刚刚尝试了很多不同的方式来尝试获得它,但所有它等于是一个空的结果,不管我做什么,如果有人可以帮助我在这个我会不胜感激,因为我只是在我的头发上撕掉我觉得它一定是我想念的那么简单的事情。如果需要更多的代码,我将使用所要求的代码编辑帖子。

+0

如果你有卷曲请求您的控制器发生什么事? –

回答

0

你index.js似乎是不完整的,但最大的问题是,你没有为.find()呼吁蒙戈回调。看看文档 - https://www.npmjs.com/package/mongodb - 看起来好像他们使用.toArray()需要回调。您需要将您的res.render放在该回调中,以便在mongo返回结果后执行。

由于节点的异步特性,到目前为止,当mongo尚未获得任何数据时,您正在执行res.render()

index.js

const express = require('express') 
const app = express() 

var MongoClient = require('mongodb').MongoClient 

app.set('view engine', 'pug') 

app.get('/test', function (req, res) { 
    var url = 'mongodb://localhost/quizmaster'; 
    //var url = 'mongodb://localhost:27017/quizmaster'; 

    var results_from_mongo = []; 

    MongoClient.connect(url, function (err, db) { 
    //var str = db.collection('qmquestions').find(); 
    var str = db.collection('qmquestions').find({}).toArray(function (err, docs){ 
     if(err){ 
     return res.send('error') 
     } 
     console.log(docs) 
     //return res.render('test', {results_from_mongo : results_from_mongo }); 
     return res.render('test', {results_from_mongo : docs }); 
    })// callback 
    //str.each(function (err, doc) { 
    // //console.log(doc); 
    // results_from_mongo.push(doc); 
    // console.log(results_from_mongo) //Push result onto results_array 
    //}); 

    //now we have a results array filled like this: 
    // results_from_mongo = ["some string", "some string", "some string"] 
    //so let's pass them to the jade file to render them. 
    }); 
}); 

app.listen('3030', function(){ 
    console.log("listening on 3030") 
}) 

的意见/ test.pug

block content 
    h1= title 
    h2= "results from mongo:" 
    select 
     // each results_from_mongos, i in results_from_mongo 
     each results_from_mongos, i in results_from_mongo 
      option(value=i) #{results_from_mongos} 
    ul 
    each item in results_from_mongo 
     li= item.question.questionTitle 
+1

是的,我忘记添加到index.js中的一些东西,比如视图引擎等等。仅仅因为我完全被强调,但是谢谢你的作品:D –

0

使用方法toArray查找方法调用后。就象这样:

collection(...).find().toArray(function(err,doc)=> 
//Do what you want 
)