简体   繁体   English

EJS 文件未按预期呈现

[英]EJS file not rendering as expected

I'm having an issue with an EJS file not rendering as expected.我遇到了 EJS 文件未按预期呈现的问题。 I'm using ExpressJS and MongoDB.我正在使用 ExpressJS 和 MongoDB。 When I fetch the data from the DB it is rendered to the page, but none of the HTML from the EJS template is being rendered.当我从数据库中获取数据时,它会呈现到页面上,但没有呈现来自 EJS 模板的 HTML。 I suspect the issue is with my router.get() call, but I've been unable to troubleshoot it thus far.我怀疑问题出在我的 router.get() 调用上,但到目前为止我一直无法对其进行故障排除。

My current code is as follows:我目前的代码如下:

Index.js索引.js

 var express = require("express"); var router = express.Router(); /* GET Userlist page */ router.get("/userlist", function (req, res) { var db = req.db; var collection = db.get("usercollection"); collection.find({}, {}, function (e, docs) { res.render("userlist", { "userlist": docs }); }); }); module.exports = router;

userlist.ejs用户列表.ejs

 <!DOCTYPE html> <html> <head> <title> User List </title> <link rel="stylesheet" href="/stylesheets/style.css" /> </head> <body> <h1> User List </h1> <ul> <% var list = ''; for (i = 0; i < userlist.length; i++) { list += '<li><a href="mailto:' + userlist[i].email + '">' + userlist[i].username + '</a></li>'; } return list; %> </ul> </body> </html>

If anyone can identify the syntax error and suggest a correction it would be greatly appreciated.如果有人可以识别语法错误并提出更正建议,我们将不胜感激。

I think the problem is in your ejs file.我认为问题出在您的 ejs 文件中。 Try this loop:试试这个循环:

<ul>
<% for (var item of userlist) { %>
    <li><a href="mailto:<%= item.email %>"><%= item.username %></a></li>
<% } %>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM