【发布时间】:2016-09-27 06:27:05
【问题描述】:
我一直在搜索如何合并来自多个集合的数据,然后使用模板助手功能客户端将其发送到模板。
我参考了Merging collections in Meteor给出的解决方案
以下是我的代码:
模板 HTML:
<template name="search_results">
<div class="col-md-12">
{{#if Template.subscriptionsReady}}
<table id="datatable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Type</th>
<th>Project Name</th>
<th>_id</th>
</tr>
</thead>
<tbody>
{{#each SearchData}}
<tr>
<td>{{search_type}}</td>
<td>{{name}}</td>
<th>{{_id}}</th>
</tr>
{{/each}}
</tbody>
</table>
{{else}}
<div class="loading">{{>spinner}}</div>
{{/if}}
</div>
</template>
模板助手功能:
Template.search_results.helpers({
SearchData: function() {
var project_name = Router.current().params.query.search;
var Projects = Projects.find({project_name: { $regex : project_name, $options:"i" } }).map( function ( Project ) { return { _id: Project._id, name: Project.project_name, search_type: 'Projects' }; }).fetch();
var CustomerAccounts = CustomerAccounts.find({account_name: { $regex : project_name, $options:"i" } }).map( function ( CustomerAccount ) { return { _id: CustomerAccount._id, name: CustomerAccount.account_name, search_type: 'Accounts' }; }).fetch();
var docs = Projects.concat(CustomerAccounts);
return docs;
}
});
我已尝试使用以下代码从单个集合发送数据,并且工作正常:
return Projects.find({project_name: { $regex : project_name, $options:"i" } }).map( function ( Project ) { return { _id: Project._id, name: Project.project_name, search_type: 'Projects' }; });
但我想合并两个集合并将数据发送到模板,我错在哪里,有什么好的方法?
【问题讨论】:
-
这个问题需要以下内容:(1) 样本输入,(2) 所需输出,(3) 当前输出,包括控制台中的任何错误。
标签: javascript mongodb meteor merge mongodb-query