2017-02-24 88 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 
)