流星Twilio MRT包 [英] Meteor Twilio MRT package

查看:120
本文介绍了流星Twilio MRT包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问(我希望) - 我正在尝试使用Meteor Twilio软件包(mrt add twilio)从我的应用程序发送短信。

Quick question (I hope) - I'm trying to use the Meteor Twilio package ("mrt add twilio") to send SMSs from my app.

一切都设置正确(我在文档中提到了预装Moment),我想......但是我得到了一个Twilio没有被定义当我的代码在事件处理程序中运行时出错。

Everything is set up correctly (and I pre-installed Moment as mentioned in the docs), I think... however I'm getting a "Twilio is not defined" error when my code is run in the event handler.

大概这与包''需要'NPM包代码的方式有关?还有其他人遇到过这种情况吗?

Presumably this is something to do with the way the package 'requires' the NPM package code? Has anyone else faced this?

Template.registration.events({
  'click #registerButton': function (e) {
    e.preventDefault();
    var accountSid = 'AC758eaf30370ee2ba8b64f64ce19769c8';
    var authToken = 'f8a0ee4560d3368a461e1f751b98fd90';

    twilio = Twilio(accountSid, authToken); //this appears to be the issue

    twilio.sendSms({
      to:'+966533444837',
      from: '+18654072438',
      body: 'Hi this is a test from Twilio.'
    }, function(err, responseData) { 
      if (!err) { 
        console.log(err)
      }
    });
  }
});


推荐答案

您使用的代码仅在服务器端运行。您需要将其与方法/调用接口

The code you have used runs only on the server side. You would need to interface it with a method/call

服务器端:

Meteor.methods({
    sendsms:function(param1, param2..) {
        var accountSid = 'AC758eaf30370ee2ba8b64f64ce19769c8';
            var authToken = 'f8a0ee4560d3368a461e1f751b98fd90';

            twilio = Twilio(accountSid, authToken); //this appears to be the issue

            twilio.sendSms({
              to:'+966533444837',
              from: '+18654072438',
              body: 'Hi this is a test from Twilio.'
            }, function(err, responseData) { 
              if (!err) { 
                console.log(err)
              }
        });
    }
});

然后在客户端..

Meteor.call("sendsms", "something", "something");

您可能需要考虑使用同步代码或包装 twilio.sendSms Meteor._wrapAsync 中,以便在客户端上获得结果。

You may need to consider using synchronous code or wrap your twilio.sendSms in Meteor._wrapAsync to get a result back on the client.

这篇关于流星Twilio MRT包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆