简体   繁体   中英

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 !!!

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);}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM