Getting undefined value at client side from Server in Meteor Solution

Posted By : Parveen Kumar Yadav | 14-Jul-2016

When we are using Meteor we all facing a problem i.e from server side the value are correct we got the value but as we return that value to client side we got undefined as in meteor documentation:-

Server method calls in Meteor are documented to be asynchronous.On the client, if you do not pass a callback and you are not inside a stub, call will return undefined, and you will have no way to get the return value of the method.

It means that when you ask for a Meteor.call method to execute remotely on the server, the local method call is non blocking and returns undefined immediately. When the method has been called on the server it will send the result asynchronously to the client, so you should retrieve it using the callback pattern. unfortunately we get undefined at client side.

So one solution for this is using Fibers/future as follows:-

Just add Fiber into your project

 Meteor npm install fibers
 

After installing just required that into your project as :-

 var Fiber = require(fibers)
 

Use future in any Meteor method as:-

 Meteor.methods({

getDataOnId: function(id){
	var fut = new Future(); 
	var result = MarketplaceProductDetails.findOne({_id:id});
	fut['return'](result);
	return fut.wait();
}
})
 

In the above example we first make object of future via new keyword. So what exactly this future is done it will wait for the database call result and not send the result until it get data from any internal call and as it gets result it will return the result

In this way you can get result at client side.

Thanks!

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..