【发布时间】:2017-03-26 06:25:57
【问题描述】:
我在我的应用程序中使用弹性搜索,一切正常,直到您单击返回 undefined 的搜索结果链接。我遇到的问题是_source._id 正在返回undefined。所以我无法查看搜索结果。
如何在/blog/article/<%= data[i]._source._id 中传递对象ID?
我的代码在下面
路由器.js
router.post('/search', function(req, res, next) {
res.redirect('/search?q=' + req.body.q);
});
router.get('/search', function(req, res, next) {
if (req.query.q) {
Article.search({
query_string: { query: req.query.q}
}, function(err, results) {
results:
if (err) return next (err);
var data = results.hits.hits.map(function(hit) {
return hit;
});
res.render('main/search-result', {
query: req.query.q,
data: data
});
});
}
});
搜索结果.ejs
<% for (var i=0; i < data.length; i++) { %>
<div class="col-md-4">
<a href="/blog/article/<%= data[i]._source._id %>">
<div class="thumbnail">
<h3 id="data_title"><%= data[i]._source.title %></h3>
</div>
</div>
<% } %>
【问题讨论】:
-
p0k8_ 你看到我的代码中缺少什么了吗?
标签: node.js mongodb elasticsearch