I have written below code to get data from mongodb :
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);
});
}
});
});
I want to pass the questions which is more than 1 to html file and want to show those questions there. I can see the questions are being fetched but I am not sure how I can display them on HTML.
can someone help?
I have added below code :
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()
});
});
But it does not print values on HTML. Below is the screenshot.
Please let me know how to proceed. Sorry for pinging a lot.
See Question&Answers more detail:os