2015-06-03 39 views
0

我試圖包裝Twilio節點客戶端,以便我可以從我的Meteor應用程序訪問和使用它。我正在使用npm-container並將twilio軟件包添加到我的packages.json文件中。然後我創建一個Meteor方法來調用並試圖包裝NPM包調用以發送SMS消息。使用wrapAsync在一個Meteor應用程序中包裹Twilio節點客戶端失敗

sendSMS: function() { 
    var twilio = Meteor.npmRequire('twilio'); 
    var client = new twilio(Meteor.settings.TWILIO.SID, Meteor.settings.TWILIO.TOKEN); 

    var options = { 
     to: Meteor.settings.TWILIO.TO, 
     from: Meteor.settings.TWILIO.FROM, 
     body: 'word to your mother.' 
    }; 

    var sms = Meteor.wrapAsync(client.sendMessage, client); 

    return sms(options); 
    } 

但是,當我打電話Meteor.call('sendSMS');從我的流星客戶端,我得到一個內部服務器錯誤...

I20150603-12:05:29.254(-4)? Exception while invoking method 'sendSMS' [object Object] 
I20150603-12:05:29.254(-4)?  at Object.Future.wait (/Users/jeremyhodges/.meteor/packages/meteor-tool/.1.1.3.1wysac9++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15) 
I20150603-12:05:29.254(-4)?  at packages/meteor/helpers.js:119:1 
I20150603-12:05:29.254(-4)?  at [object Object].Meteor.methods.sendSMS (app/server/index.js:229:12) 
I20150603-12:05:29.254(-4)?  at [object Object].methodMap.(anonymous function) (packages/meteorhacks:kadira/lib/hijack/wrap_session.js:160:1) 
I20150603-12:05:29.254(-4)?  at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1) 
I20150603-12:05:29.255(-4)?  at packages/ddp/livedata_server.js:648:1 
I20150603-12:05:29.255(-4)?  at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) 
I20150603-12:05:29.255(-4)?  at packages/ddp/livedata_server.js:647:1 
I20150603-12:05:29.255(-4)?  at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) 
I20150603-12:05:29.255(-4)?  at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1) 

回答

1

Twilio是拋出一個錯誤。您將要調查進一步調試:

try { 
    var sms = Meteor.wrapAsync(client.sendMessage, client); 

    return sms(options); 
}catch(e) { 
    console.log(e.message) 
} 

e.message應包含有關問題的詳細信息。這也與client.sendMessage的回調中的第一個參數(通常是錯誤)給出的信息相同

1

使用Async.wrap

if (Meteor.isServer) { 

    var twilio = Meteor.npmRequire('twilio'); 
    var client = twilio(Meteor.settings.TWILIO.SID, Meteor.settings.TWILIO.TOKEN); 
    var wrappedClient = Async.wrap(client, ['sendMessage']); 

    Meteor.methods({ 

    sendSMS: function(){ 

     var options = { 
     to: Meteor.settings.TWILIO.TO, 
     from: Meteor.settings.TWILIO.FROM, 
     body: 'word to your mother.' 
     } 

     return wrappedClient.sendMessage(options); 

    } 

    }) 

} 
相關問題