Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing value server to client in method meteor

client side code

Template.hello.events({
 "click": function () {
  Meteor.call('Message',function(result){
  alert(result);
 });

server side method call

if (Meteor.isServer) {

Meteor.methods({
 'Message':function(){
 SerialPort.list(function (err, ports) {

 ports.forEach(function(port) {
 console.log(port.comName);  
  var atxt =  port.comName;
  return atxt ;
 });//ports end
 });  //list end
 }   //message end
 }); //method end
 }   //server end

The above programme 'undefined' print the alert box client side . return atxt not returned any value. please help me meteor apllication passing server side return value access the client side !!!

like image 448
Aboobakkar P S Avatar asked May 17 '26 23:05

Aboobakkar P S


1 Answers

when Meteor server side methods returns some data, client side need to fetch that asynchronously via a callback. And in that callback, there need to have two parameters, error and result. Basically the second parameter is your result and the first one is error (if any). So, you need to update your client side code accordingly. Besides before returning from server side ,just console.log your data so that you can be sure of the result.

Meteor.call('Message',function(err,result){
    if(!err) {
        alert(result);
    } else {console.log(err);}
});
like image 68
Faysal Ahmed Avatar answered May 20 '26 11:05

Faysal Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!