How to properly use server-side functions with helpers?


#1

Hello,

Currently, I try to make some database calls server-side due better performance. I built this test:

    <md-button ng-click="view1.callTestfunction()"> Test!</md-button>

    <h1>{{view1.getTestData}}</h1>

This is my client-side code:

//some code skipped
class View1 {

 constructor($interval, $scope, $reactive ) {
        'ngInject';
        $reactive(this).attach($scope);
        this.helpers({
            getTestData(){
                Meteor.call('allTestData',function(error, result){
                    if(error){
                        alert('Error');
                    }else{
                       return result;
                    }
                });
            }
        });

    }
    callTestfunction(){
        Meteor.call('allTestData',function(error, result){
            if(error){
                alert('Error');
            }else{
                console.log(result);
            }
        });

    }

This is my code on the server side:

//some code skipped
Meteor.methods({
        allTestData:()=>{
       var results=Kafkadata.find().count();
        console.log(results);
        return results;
    },

});

If I click on the test button it shows me the count of the db entries. But I dont want to use with a button. I want to use it with a helper. But view1.getTestData doesnt show anything. Is this the right way to use meteor methods with a helper? What am I doing wrong here?


#2

Well. I think the problem has something to do with async calls?


#3

I dont know if its the right way (all people in this forum seems to be dead, because no one answers), but I got a working solution now.

Instead of returning the result with return result in my helpers function, I used Session.set('test', result) and Session.get('test').
It looks like this:

            getTestData(){
                Meteor.call('allTestData',function(error, result){
                    if(error){
                        alert('Error');
                    }else{
                       Session.set('test',result);
                    }
       }
)
return Session.get('test');
},

If ‘Session’ is unknown, you have to add this thing with meteor add session.

Whether my questions are too difficult or too silly. I dont know…