2015-10-21 17 views
1

我安裝了meteorhacks/npm,以便在我的Meteor應用程序中使用Wordcount包。如何使用meteorhacks調用此wordcount函數:npm包?

但是,我不能讓我的方法工作。

客戶

getWordcount = function getWordcount(words, callback) { 
    Meteor.call('getWordcount', words, callback); 
    } 

console.log(getWordcount('hello world')); // testing 

服務器

Meteor.methods({ 
    'getWordcount': function getWordcount(words) { 
     var WordcountApi = Meteor.npmRequire('wordcount'); 
     var wordcount = new WordcountApi({ 
      version: "1.1.1" 
     }); 

     var words = Async.runSync(function(done) { 
     wordcount.words, function(err, data) { 
      done(null, data); 
     } 
     }); 

     return words.result; 
    } 
    }); 

我回來在我的控制檯發出錯誤訊息:

「錯誤調用方法 'getWordcount':內部服務器錯誤[500 ]「

回答

1

我的建議

客戶

// call meteor method and catch err or results in a callback function 
Meteor.call('getWordcount', 'hello world', function(err, results){ 
    if(err) console.error(err); 
    else console.log(results); 
}); 

服務器

Meteor.methods({ 
     'getWordcount': function getWordcount(words) { 
      check(words, String); 
      var wordcount = Meteor.npmRequire('wordcount'); 
      return wordcount(words); 
     } 
    });