Search code examples
node.jsmeteormeteor-blaze

Meteor Blaze iterate over dynamically created object


I'm trying to iterate with Blaze over dynamically created object. I have the collection and the object, but I can't display the data in my tables and I don't understand why.

Here's my code to retrieve the object :

In my Template Helpers :

fields: ()=> {
    var collectionSelected = FlowRouter.current().params.collectionName;

    // var data = Mongo.Collection.get(collectionSelected).find().count();


    var collectionObject = Collections.findOne({name: collectionSelected});

    console.log(Collections.findOne({name: collectionSelected}));

    return Collections.findOne({name: collectionSelected});
},

values: ()=> {

    var collectionSelected = FlowRouter.getParam('collectionName');
    var collectionObject = Collections.findOne({name: collectionSelected});
    var dataArray = [];

    var data = Meteor.call('findElmt', collectionSelected, function(err, res){
        console.log('VALUES : ');
        console.log(res);
        return res;
    });

},

The findElmt method :

findElmt(collectionName){
    if (global.hasOwnProperty(collectionName)){
        var res = Mongo.Collection.get(collectionName).find().fetch();

        console.log(res);
        return res;
    }

    else {
        global[collectionName] = new Meteor.Collection(collectionName);

        var res = Mongo.Collection.get(collectionName).find().fetch();
        console.log(res);
        return res;
    }

}

And finally the way I'm trying to display it with Blaze :

<table data-toggle="table" data-show-columns="true" data-search="true" data-pagination="true">
    {{#with fields}}
        {{#if Template.subscriptionsReady}}
            <thead>
                <tr>
                {{#each fields.fieldsName}}
                    <th data-sortable="true">{{this}}</th>
                {{/each}}
                </tr>
            </thead>
        {{else}}
            <p>Loading...</p>
        {{/if}}
    {{/with}}
    <tbody>
        {{#each values}}
            <tr>
            {{#each valueString in values.items}}
                <td>{{valueString}}</td>
            {{/each}}
            </tr>
        {{/each}}
    </tbody>
</table>

If you have any advice on what I'm doing wrong, thank you !

EDIT 1 : I added the fields helper !


Solution

  • Thank you a lot for your help, I finally nailed it.

    Here's the final code for the future lost soul like me !

    Template Helpers :

    fields: ()=> {
        var collectionSelected = FlowRouter.current().params.collectionName;
    
        // var data = Mongo.Collection.get(collectionSelected).find().count();
    
    
        var collectionObject = Collections.findOne({name: collectionSelected});
    
        console.log(Collections.findOne({name: collectionSelected}));
    
        return Collections.findOne({name: collectionSelected});
    },
    
    values: ()=> {
    
        var collectionSelected = FlowRouter.getParam('collectionName');
        var collectionObject = Collections.findOne({name: collectionSelected});
        var dataArray = [];
    
        console.log(ReactiveMethod.call("findElmt", collectionSelected));
    
        return ReactiveMethod.call("findElmt", collectionSelected);
    }
    

    The server side FindElmt method didn't change, and finally the Blaze code :

    <table data-toggle="table" data-show-columns="true" data-search="true" data-pagination="true">
                        {{#with fields}}
                            {{#if Template.subscriptionsReady}}
                                <thead>
                                    <tr>
                                    {{#each fields.fieldsName}}
                                        <th data-sortable="true">{{this}}</th>
                                    {{else}}
                                        {{! will this fix this issue? }}
                                    {{/each}}
                                    </tr>
                                </thead>
                            {{else}}
                                <p>Loading...</p>
                            {{/if}}
                        {{else}}
                            {{! will this fix this issue? }}
                        {{/with}}
    
                        {{#with values}}
                            <tbody>
                            {{#each values}}
                                <tr>
                                {{#each valueString in this.items}}
                                    <td>{{valueString}}</td>
                                {{else}}
                                    {{! will this fix this issue? }}
                                {{/each}}
                                </tr>
                            {{else}}
                                {{! will this fix this issue? }}
                            {{/each}}
                            </tbody>
                        {{else}}
                            {{! will this fix this issue? }}
                        {{/with}}
    
                    </table>