我有一个结果变量,它是一个对象数组。我将结果变量从我的 javascript 文件传送到我的主路由文件。我正在尝试渲染我的页面以显示模板 ejs 文件中每个对象的列表。我能够很好地列出所有内容,但是列表以 [object object] 的形式出现,而不是对象中的实际单词。如何让它在我的模板文件中显示为字符串?
这是我的路由文件:
app.get('/announcement', function(req,res){
var getAnnouncements = require('../public/javascripts/announcement.js'); //Load the module, get the name of the script file
//define the functions here
var onSpreadsheetSuccess = function (results) { //result is announcementArray
//add results list to template);
res.render('announcement', {title: "Announcement page", results: results});
}
getAnnouncements.loadSheet(onSpreadsheetError, onSpreadsheetSuccess); //call the function from script with the parameters passed
})
这就是我在模板 ejs 文件中所做的:
<ul>
<% results.forEach(function(result){ %>
<li><%= result %></li>
<% }); %>
</ul>
最佳答案
<ul>
<% for(var i=0; i<results.length; i++) { %>
<li>
<%= results[i] %>
</li>
<% } %>
</ul>
关于node.js - 如何在模板ejs中排列对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29190925/