按索引 returns 未定义的数组输出
Output from array by index returns undefined
我正在尝试输出位于 json 对象中的数组的特定索引。
例如,我从 MongoDB
中获取了以下 json 对象
{ _id: 55b65199c92d15d80fd94803,
site: 'test.com',
company: 'test',
__v: 0,
votes: [
{ vote_user: '55a11de3e4c770982730ccb9' },
{ vote_user: '55a11de3e4c770982730612a' }
]
}
此对象作为名为 suggestion 的对象从 nodejs route render 传递,并通过 EJS 循环使用
<% suggestion.forEach(function(item){ %><%}%>
在这个阶段,我尝试使用
从 votes 数组中检索特定的 vote_user 对象
<% suggestion.forEach(function(item){ %><%- item.votes[0] %><%})%>
然而,我得到的只是未定义
我尝试将 'item' 分配给 javascript 变量,然后将其记录到浏览器控制台,它完全按照我的意愿和期望工作 - 正如我得到的以下结果:
Object {vote_user: "55a11de3e4c770982730ccb9"}
真的无法理解为什么它不能像我假设的那样工作。
编辑
由于不太清楚设置的样子,下面是实际使用的代码:
EJS
<% suggestion.forEach(function(item, i){ %>
<div class="suggestion-entry suggestion-<%- item._id %>" data-entry="<%- item._id %>">
<!--<%- item %>-->
<div class="entry-info">
<div class="voting">
<script>
console.log(<%- item.votes %>[0].vote_user)
</script>
<%- item.votes[0].vote_user %>
<div class="vote vote-up"><a><i class="fa fa-chevron-up"></i></a></div>
<div class="vote vote-down"><a><i class="fa fa-chevron-down"></i></a></div>
</div>
<div class="image"><img src='http://placehold.it/130x90.jpg'></div>
<div class="company">
<h2><a href="<%= item.site %>"><%= item.company %></a></h2>
<span class="desc"><%= item.company %></span>
</div>
</div>
</div>
<% }); %>
脚本标签输出实际的 vote_user 值而不是未定义的值。
Nodejs 路由
app.get('/', function(req, res) {
Suggestion.find(function(err, suggestions){
res.render('index', { message: req.flash('message'), title: "title", user : req.user, suggestion : suggestions });
})
});
MongoDB文档如开头所写
已编辑问题的答案:
首先,您的 id
需要是一个字符串。问题似乎出在使用 <%-
标签上。也许您可以找到关于它应该如何工作的文档,但在网站上进行测试时,它根本不起作用,而 <%=
完美运行。精简的概念证明:
<%
var suggestion = [{ _id: '55b65199c92d15d80fd94803',
site: 'test.com',
company: 'test',
__v: 0,
votes: [
{ vote_user: '55a11de3e4c770982730ccb9' },
{ vote_user: '55a11de3e4c770982730612a' }
]
}];
%>
<% suggestion.forEach(function(item, i){ %>
<div class="suggestion-entry suggestion-<%= item._id %>" data-entry="<%= item._id %>">
<li><%= item.votes[0].vote_user %></li>
<li><%= item.site %></li>
<li><%= item.company %></li>
</div>
<% }); %>
将它粘贴到网站上的测试文本区域,您会看到它可以正常工作。如果这仍然不适用于您的代码,则说明发生了一些完全不同的事情。
所以,我终于设法解决了这个问题 - 我不确定为什么这个解决方案有效,但它确实有效。
根据@Jan 的建议,id
必须转换为字符串以使 EJS 能够正确验证和使用 json 对象。
解决方案是首先对从节点
传递的json对象进行字符串化
app.get('/', function(req, res) {
Suggestion.find(function(err, suggestions){
res.render('index', { message: req.flash('message'), title: "title", user : req.user, suggestion : JSON.stringify(suggestions) });
})
});
这 returns 所有对象和数组都是字符串,而不是为 mongo 中的值设置的数据类型。
为了能够使用字符串值,然后我在 EJS 中简单地将字符串化建议变量解析为 JSON 和
JSON.parse(suggestion)
我正在尝试输出位于 json 对象中的数组的特定索引。
例如,我从 MongoDB
中获取了以下 json 对象{ _id: 55b65199c92d15d80fd94803,
site: 'test.com',
company: 'test',
__v: 0,
votes: [
{ vote_user: '55a11de3e4c770982730ccb9' },
{ vote_user: '55a11de3e4c770982730612a' }
]
}
此对象作为名为 suggestion 的对象从 nodejs route render 传递,并通过 EJS 循环使用
<% suggestion.forEach(function(item){ %><%}%>
在这个阶段,我尝试使用
从 votes 数组中检索特定的 vote_user 对象 <% suggestion.forEach(function(item){ %><%- item.votes[0] %><%})%>
然而,我得到的只是未定义
我尝试将 'item' 分配给 javascript 变量,然后将其记录到浏览器控制台,它完全按照我的意愿和期望工作 - 正如我得到的以下结果:
Object {vote_user: "55a11de3e4c770982730ccb9"}
真的无法理解为什么它不能像我假设的那样工作。
编辑
由于不太清楚设置的样子,下面是实际使用的代码:
EJS
<% suggestion.forEach(function(item, i){ %>
<div class="suggestion-entry suggestion-<%- item._id %>" data-entry="<%- item._id %>">
<!--<%- item %>-->
<div class="entry-info">
<div class="voting">
<script>
console.log(<%- item.votes %>[0].vote_user)
</script>
<%- item.votes[0].vote_user %>
<div class="vote vote-up"><a><i class="fa fa-chevron-up"></i></a></div>
<div class="vote vote-down"><a><i class="fa fa-chevron-down"></i></a></div>
</div>
<div class="image"><img src='http://placehold.it/130x90.jpg'></div>
<div class="company">
<h2><a href="<%= item.site %>"><%= item.company %></a></h2>
<span class="desc"><%= item.company %></span>
</div>
</div>
</div>
<% }); %>
脚本标签输出实际的 vote_user 值而不是未定义的值。
Nodejs 路由
app.get('/', function(req, res) {
Suggestion.find(function(err, suggestions){
res.render('index', { message: req.flash('message'), title: "title", user : req.user, suggestion : suggestions });
})
});
MongoDB文档如开头所写
已编辑问题的答案:
首先,您的 id
需要是一个字符串。问题似乎出在使用 <%-
标签上。也许您可以找到关于它应该如何工作的文档,但在网站上进行测试时,它根本不起作用,而 <%=
完美运行。精简的概念证明:
<%
var suggestion = [{ _id: '55b65199c92d15d80fd94803',
site: 'test.com',
company: 'test',
__v: 0,
votes: [
{ vote_user: '55a11de3e4c770982730ccb9' },
{ vote_user: '55a11de3e4c770982730612a' }
]
}];
%>
<% suggestion.forEach(function(item, i){ %>
<div class="suggestion-entry suggestion-<%= item._id %>" data-entry="<%= item._id %>">
<li><%= item.votes[0].vote_user %></li>
<li><%= item.site %></li>
<li><%= item.company %></li>
</div>
<% }); %>
将它粘贴到网站上的测试文本区域,您会看到它可以正常工作。如果这仍然不适用于您的代码,则说明发生了一些完全不同的事情。
所以,我终于设法解决了这个问题 - 我不确定为什么这个解决方案有效,但它确实有效。
根据@Jan 的建议,id
必须转换为字符串以使 EJS 能够正确验证和使用 json 对象。
解决方案是首先对从节点
传递的json对象进行字符串化app.get('/', function(req, res) {
Suggestion.find(function(err, suggestions){
res.render('index', { message: req.flash('message'), title: "title", user : req.user, suggestion : JSON.stringify(suggestions) });
})
});
这 returns 所有对象和数组都是字符串,而不是为 mongo 中的值设置的数据类型。
为了能够使用字符串值,然后我在 EJS 中简单地将字符串化建议变量解析为 JSON 和
JSON.parse(suggestion)