【发布时间】:2015-05-25 07:15:25
【问题描述】:
我有一个结果变量,它是一个对象数组。我将结果变量从我的 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>
【问题讨论】:
-
你解决了吗?我也面临类似的问题!!!运行时出现数组未定义错误