I have written below code to read values from mongodb using mongoose and store it in array:
app.get('/StudentQuestionsPage',function(req,res){
var allQuestionsArray = studentQuestions.find();
var questions = [];
allQuestionsArray.exec(function(err,questions){
if(err)
return cosole.log(err);
questions.forEach(function(question){
var elem = new Object();
elem["id"] = question.id;
elem["quesStatement"] = question.quesStatement;
elem["optionA"]=question.optionA;
elem["optionB"]=question.optionB;
elem["optionC"]=question.optionC;
elem["optionD"]=question.optionD;
questions.push(elem);
console.log(elem)
});
});
res.render(__dirname + '/StudentQuestionsPage.html',{questions:questions});
});
I have to pass this array of questions to an html file and then I have to show the content on HTML.
I have written below code to show array values on HTML but it does not show anything. "console.log(elem)" can print values on console.
<html>
<head>
<title>Online Examination Portal</title>
<h1>Questions</h1>
</head>
<body>
<div>
<form>
<h2>Questions</h2>
<ul>
<% questions.forEach(function(question) { %>
<li>Number: <%= question.id %> <br/> Text: <%= question.quesStatement %></li>
<% }); %>
</ul>
</form>
</div>
</body>
</html>
Please let me know how can I pass the values from nodejs and retrieve and show it on html.