流星-合并多个集合中的数据 [英] Meteor - Merging Data from multiple Collections
问题描述
我一直在大量研究如何合并来自多个集合的数据,然后使用模板帮助器功能"客户端将其发送到模板.
I have been doing lots of search on how to merge data from multiple collections and then send it to the Template using the Template Helper Function client side.
我提到了以下解决方案:在流星中合并集合
I referred to the solution given at : 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;
}
});
我尝试使用以下代码从单个集合中发送数据并正常工作:
I have tried the following code to send data from the single collection and its working fine:
return Projects.find({project_name: { $regex : project_name, $options:"i" } }).map( function ( Project ) { return { _id: Project._id, name: Project.project_name, search_type: 'Projects' }; });
但是我想合并两个集合并将数据发送到Template,我哪里做错了,做这件事的好方法是什么?
But I want to merge two collections and send the data to Template, Where I am wrong and what is the good way to do it?
推荐答案
A cursor's map function returns an array, so calling fetch
on that array would throw an error. Try removing the fetch
calls and see if that helps.
这篇关于流星-合并多个集合中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!