Meteor - Merging Data from multiple Collections

2019-09-09 22:11发布

问题:

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

Below is my Code:

Template 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 Helper Function:

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' }; });

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?

回答1:

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.



回答2:

I was doing one Typo Mistake Now the Issue is corrected:

Check My Rectified Code Below

    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' }; });

    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' }; });

    var docs = projects.concat(customeraccounts);

    return docs;

I have created variable name same as Collection Name, then reailsed and then changed it to lowercase, instead of var Projects I changed it to var projects,

Second thing I have also removed fetch()

The issue is Solved now , As David Suggested me to remove fetch() , I am marking his Answer as Correct although my code has another issue of variable name.