2016-02-24 122 views
4

客戶端代碼傳遞值服務器到客戶端在方法流星

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

服務器側方法調用

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 

上述程序「未定義」打印警告框客戶端。返回atxt沒有返回任何值。請幫我流星 apllication傳遞服務器端返回值訪問客戶端!!!

回答

1

當流星server side方法返回一些數據時,客戶端需要通過回調獲取asynchronously。在這個回調中,需要有兩個參數,error and result。基本上第二個參數是你的結果,第一個參數是error(如果有的話)。所以,你需要相應地更新你的客戶端代碼。除了在從服務器端返回之前,只需要console.log您的數據,以便您可以確定結果。

Meteor.call('Message',function(err,result){ 
    if(!err) { 
     alert(result); 
    } else {console.log(err);} 
}); 
+1

非常感謝faysal ...... –

相關問題