我正在建立一个用于记录和查看患者数据的网站。在服务器端,我使用的是 Node.js+express,我正在使用 ejs 渲染页面。我能够毫无问题地转移到页面内的数据。但是,我一直在解析 JSON 数据。在我的代码中:
// Get all patients in the system
app.get('/patients', function (req, res, next) {
console.log("ALL PATIENTS PAGE");
getRequestCounter++;
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
Patient.find({},function(err, data){
console.log("DATA IS HERE" + data);
if(err) throw err;
res.render("patients", {collection: data});
});
console.log('received GET request.');
})
我从 MongoDb 获取所有患者信息,并将其作为原始数据传递给 patients.ejs 页面。
<div class="middle">
<h2> Patients Information</h2>
<p> <%= collection%></p>
</div>
当我通过它时,输出如下所示:输出
所以,我想知道一种有效且很好的方法来解析 JSON 数据,它在 HTML 中看起来很漂亮。非常感谢!