twilio发送短信不在流星中工作

时间:2017-08-13 02:58:46

标签: javascript node.js meteor twilio twilio-api

我正在使用twilio节点发送短信。但是我收到了错误:

  

sendSms未定义

这是我在服务器文件夹中的twilio文件:

import twilio from "twilio";
sms = {
    accountSid: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    authToken: "your_auth_token"
};

const client = new twilio(sms.accountSid, sms.authToken);
console.log('client twilio *********** ',client)
sendSms=(phoneNum,randomNum)=> {
    client
    .sendSms({
        body: "MicroTM one time password:" + randomNum,
        to: phoneNum,
        from: "+16062631146"
    })
    .then((message, err) => {
        if (!err) {
            console.log(message);
        } else {
            console.log(err);
        }
    });
}

现在当我控制twilio时,我无法找到sendSms功能。 这是日志:

client twilio ***********  Twilio {
I20170813-08:14:44.200(5.5)?   username: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
I20170813-08:14:44.201(5.5)?   password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
I20170813-08:14:44.202(5.5)?   accountSid: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
I20170813-08:14:44.204(5.5)?   httpClient: {},
I20170813-08:14:44.204(5.5)?   region: undefined,
I20170813-08:14:44.205(5.5)?   _accounts: undefined,
I20170813-08:14:44.206(5.5)?   _api: undefined,
I20170813-08:14:44.207(5.5)?   _chat: undefined,
I20170813-08:14:44.208(5.5)?   _fax: undefined,
I20170813-08:14:44.209(5.5)?   _ipMessaging: undefined,
I20170813-08:14:44.209(5.5)?   _lookups: undefined,
I20170813-08:14:44.210(5.5)?   _monitor: undefined,
I20170813-08:14:44.210(5.5)?   _notify: undefined,
I20170813-08:14:44.211(5.5)?   _preview: undefined,
I20170813-08:14:44.211(5.5)?   _pricing: undefined,
I20170813-08:14:44.212(5.5)?   _taskrouter: undefined,
I20170813-08:14:44.212(5.5)?   _trunking: undefined,
I20170813-08:14:44.212(5.5)?   _video: undefined,
I20170813-08:14:44.213(5.5)?   _messaging: undefined,
I20170813-08:14:44.213(5.5)?   _wireless: undefined,
I20170813-08:14:44.214(5.5)?   _sync: undefined }

这是我的package.json文件:

{
  "name": "MicroTM",
  "private": true,
  "scripts": {
    "start": "meteor run"
  },
  "dependencies": {
    "babel-runtime": "^6.20.0",
    "bcrypt": "^1.0.2",
    "busboy": "^0.2.14",
    "fibers": "^2.0.0",
    "lodash": "^4.17.4",
    "meteor-node-stubs": "~0.2.4",
    "moment": "^2.18.1",
    "twilio": "^3.6.2"
  }
}

package.json文件可能存在一些问题,因为我无法找到twilio api的任何问题。

编辑:

这是我的客户端,我调用了send sms函数:

Template.register.events({
    'submit form': function(event) {
        event.preventDefault();
        let fullName = event.target.fullName.value,
            phoneNum = event.target.phoneNum.value,
            email = event.target.emailsignup.value,
            password = event.target.passwordsignup.value,
            confirmPass = event.target.passwordsignup_confirm.value;
            console.log(phoneNum,email,password,confirmPass);
        let randomNum = Math.floor(1000 + Math.random() * 9000);
        let data = {
            fullName: fullName,
            phoneNum: phoneNum, 
            email:email, 
            password:password, 
            confirmPass:confirmPass,
            otp:randomNum,
            isVerified: false,
            createdAt: Date.now()
        };
        if(password != confirmPass){
            swal({
              title: 'passwords are not matching!',
            })
        }
        else{
            Meteor.call('registerUser',data,function (err,res) {
                if(!err){
                    //console.log('inside result ******* ',data)
                    sendSms(data.phoneNum,randomNum);
                    Router.go('verify')
                }
                else{
                    console.log('error ******* ', err)
                }
            })
        }
    }
})

还请找到我收到的错误的屏幕截图: enter image description here

2 个答案:

答案 0 :(得分:3)

使用twilio的其他方法时遇到同样的问题。

尝试使用方法client.messages.create()

client.messages.create({
        body: "MicroTM one time password:" + randomNum,
        to: phoneNum,
        from: "+16062631146"
    })
    .then((message, err) => {
        if (!err) {
            console.log(message);
        } else {
            console.log(err);
        }
    });

另外请务必使用较新版本的twilio并保存版本以打包:

npm install twilio --save

编辑:此外,由于该特定文件中的方法无法使用,您似乎正在从无法访问的文件中调用sendSms method。尝试检查您正在访问的地点sendSms的功能范围。或者尝试在此处发布更多代码以了解更多信息。

EDIT2:您无法调用客户端服务器上定义的方法,但您可以确保使用ajaxsocket.io发送任何事件,并将参数从客户端传递到服务器。收到服务器后,您可以致电并将您的参数传递给sendSms方法。这可以帮助您。

答案 1 :(得分:1)

以这种方式声明sendSms函数,使其仅在当前范围内可见。

将其替换为:

Meteor.sendSms=(phoneNum,randomNum)=> {
  ...
}

它可以在任何地方访问。