javascript - twilio 发送短信在 meteor 中不起作用

标签 javascript node.js meteor twilio twilio-api

我正在使用 twilio Node 发送短信。但我收到错误:

sendSms is not defined

这是我在服务器文件夹中的 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 的任何问题。

编辑:

这是我调用发送短信功能的客户端:

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

最佳答案

使用 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 方法。尝试检查您正在访问 sendSms 的位置的函数范围。或者尝试在此处发布更多代码以了解更多信息。

编辑2:您无法在客户端调用服务器上定义的方法,但您肯定可以使用 ajax 发送任何事件。或socket.io并将参数从客户端传递到服务器。在服务器上接收后,您可以调用并将参数传递给 sendSms 方法。也许这对您有帮助。

关于javascript - twilio 发送短信在 meteor 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45656664/

相关文章:

javascript - Nodeschool learnyounode 过滤器 LS 练习 : Cannot read property 'toString' of undefined

javascript - 从 MVC View 访问 DLL 中的 SaveMethod

javascript - 当使用新 Prop 调用渲染时,React JS 未完全更新 View

javascript - 从 express.js 中删除所有 header

javascript - JSDOM:删除一次嵌套的 block 引用但留下 2+ 嵌套的 block 引用

javascript - 调用方法时出错 'addReservation' : Internal server error [500]

javascript - 使用 Mantra 从 Meteor 中的回调方法传递值

javascript - 用于保存下拉值的 session 存储

javascript - LESS — data-uri 画家 mixin

javascript - 获取 meteor 中语义 Accordion 的当前状态