2017-08-13 58 views
0

我使用twilio節點發送短信。但我得到的錯誤:twilio發送短信不工作在流星

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" 
    } 
} 

,因爲我無法找到與twilio API的任何問題,可能有一些問題與的package.json文件。

編輯:

這裏是我的客戶端,我叫的發送短信功能:

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

+0

確保客戶端可用的功能 –

+0

如何在客戶端調用服務器功能? – wrangler

+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方法。可以幫助您。

+0

我很欣賞你的答案,但仍然沒有運氣....得到相同的控制檯。嘗試了client.messages.create和npm安裝twilio --save –

+0

我從客戶端(前端)傳遞這些參數,這些工作正常。 –

+0

您可以編輯您的答案,以便我能夠了解更多 –

1

您聲明的sendSms功能,所以它只會在當前範圍內可見。

將其替換爲:

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

這將是任何地方訪問。

+0

服務器上不存在窗口我猜 –

+0

@AnkushRishi,哦,謝謝,你說得對。我編輯了我的答案。 – Styx

+0

它使用流星運行,但仍然沒有sendSms函數。可能是這是一個範圍錯誤 –