Parveen yadav
Parveen yadav

Reputation: 2362

How to render data coming from Server side (DB call) Meteor method to template Meteor

I am using Meteor in one of my project where i need to simply show the data on meteor template say inventory.html. I have a method at server side which hit a query and get the data from mongoDb . I call the server side method from client side and getting all the data at client side as well but as the data coming to client side takes some time on the mean time the template is rendered without any values so how would i show the values in the template or there is any technique so that i can show my values or data.

Currently i am using a service to setting and getting data i.e testService.

var testservice = require('./TestService');

Template.allInventory.rendered = function() {


Template.allInventory.helpers({
 productDetails: function() { 

    return testservice.getData();
}
})
}

channels.js where i am setting the data coming from DB

var testservice = require('./TestService');
Template.channels.rendered = function(){

    Meteor.call('getDetialsFromDB',function(err, res){
    if(res){
        console.log("res is...getAllProductDetail.."+res);
        testservice.setData(res);
    }
    if(err){
        console.log("Error while calling getAllProductDetail");
    }
})

if i call the above method without using the service than its render the template without any data because data coming from backend taking some time like:

Template.allInventory.rendered = function() {
Template.allInventory.helpers({
 productDetails: function() { 
 var data;
Meteor.call('getDetialsFromDB',function(err, res){
    if(res){
        console.log("res is...getAllProductDetail.."+res);
        data = res;
    }
    if(err){
        console.log("Error while calling getAllProductDetail");
    }
})
   return res;
}
})

so i just want to render my data coming from one of Meteor .method of server side calling from client side or please give any example

Any help would be appriciated! Thanks

Upvotes: 0

Views: 335

Answers (2)

khem poudel
khem poudel

Reputation: 587

Template.someTemplate.onCreated(function(){
 this.someVar = new ReactiveVar(null)
 let self = this
 Meteor.call('someMethod',function(err,res){
  if(!err){
   self.someVar.set(res)
  }  
 })
})

We captured the result of meteor method into reactive variable so as to execute helper function until the method retrieves the result
now make helpers to get the result of the meteor method

Template.someTemplate.helpers({
 getSomeVar:function(){
  if(Template.instance().someVar.get())
   return Template.instance().someVar.get()
  }
})

Upvotes: 0

Anis D
Anis D

Reputation: 761

You can use SSR Package for server side rendering.

Upvotes: 0

Related Questions