-2
我已經寫了下面的代碼從MongoDB中獲取數據:的NodeJS,MongoDB的:通過獲取數據並顯示它在HTML
app.get('/StudentQuestionsPage',function(req,res){
var questions="";
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
}
else {
console.log('Connection established to', url);
var collection = db.collection('studentQuestions');
var cursor =collection.find();
fs.readFile(__dirname + '/StudentQuestionsPage.html', 'utf8', function(err, content) {
var result = content;
cursor.each(function (err, doc) {
if (err) {
console.log(err);
} else {
result +=doc;
}
});
res.send(result);
});
}
});
});
我想通過這比1到HTML文件中的問題,並希望在那裏顯示那些問題。我可以看到問題正在提取,但我不確定如何在HTML上顯示它們。
有人可以幫忙嗎?
我已經添加下面的代碼:
app.get('/StudentQuestionsPage',function(req,res){
var studentQuestions = mongoose.model('studentQuestion', studentQuestionSchema);
fs.readFile(__dirname + '/StudentQuestionsPage.html', 'utf8', function(err, content) {
var result = content;
res.send(result).status({studentQuestions:studentQuestions});
mongoose.connection.close()
});
});
請讓我知道如何繼續。對不起,很多。
我正在讀取來自MongoDB集合的值。我將它存儲到遊標中,並且值必須傳遞給HTML。你給出的例子已經硬編碼了數組的值,但在我的情況下,它們是從mongodb中讀取的。 – Abhinav
我知道..我已經離開你了..因爲你的問題問如何顯示在HTML頁面上,你說你已經能夠獲取它們。所以,只要結合你和這個,你應該很好去.. –
好吧,現在我有一個更新的版本...請參閱nodeJs資源如何鏈接mongo數據庫,如果你仍然有問題。我猜想這是一個簡單的步驟 –