我为这个提交的可怕标题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" } }
我刚刚尝试了很多不同的方式来尝试获得它,但所有它等于是一个空的结果,不管我做什么,如果有人可以帮助我在这个我会不胜感激,因为我只是在我的头发上撕掉我觉得它一定是我想念的那么简单的事情。如果需要更多的代码,我将使用所要求的代码编辑帖子。
如果你有卷曲请求您的控制器发生什么事? –