[英]NodeJS: undefined is not a new function 'new'
我正在嘗試Node JS Twilio指南中的一小段代碼: https ://www.twilio.com/blog/2013/03/introducing-the-twilio-module-for-node-js.html
var twilio = require('twilio')('AUTH-ID','AUTH-SECRET');
http = require('http');
http.createServer(function (req, res) {
var resp = new twilio.TwimlResponse();
resp.say({voice:'woman'}, 'ahoy hoy! Testing Twilio and node.js');
res.writeHead(200, {
'Content-Type':'text/xml'
});
res.end(resp.toString());
}).listen(1337);
console.log('Visit http://localhost:1337/ in your browser to see your TwiML document!');
啟動此代碼段並訪問URL時,得到以下響應:
/Users/unicornherder/Desktop/Porter/inbound.js:7
var resp = new twilio.TwimlResponse();
^
TypeError: undefined is not a function
at Server.<anonymous> (/Users/unicornherder/Desktop/Porter/inbound.js:7:16)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:527:27)
有人可以請我解釋我做錯了嗎?
您需要保存require('twilio')
導出的require('twilio')
,因為它是TwimlResponse
所在的TwimlResponse
,而不是保存在當前接收的客戶端對象上( require('twilio')(..)
與require('twilio').RestClient(..)
)。 因此改為:
var http = require('http');
var twilio = require('twilio');
// or `var twilioClient = twilio(...)`
var twilioClient = new twilio.RestClient('AUTH-ID','AUTH-SECRET');
http.createServer(function (req, res) {
var resp = new twilio.TwimlResponse();
resp.say({voice:'woman'}, 'ahoy hoy! Testing Twilio and node.js');
res.writeHead(200, {
'Content-Type':'text/xml'
});
res.end(resp.toString());
}).listen(1337);
FWIW twilio
文檔顯示了以這種方式使用模塊的示例(將導出與實際的Rest客戶端分離)。
從導入中刪除參數“ AUTH-ID”和“ AUTH-SECRET”。
如果在導入過程中傳遞了這些參數,則將初始化其余客戶端,並且twilio變量將不表示模塊對象。 因此,TwimlResponse對象將是未定義的。
我正在看那個博客,看來如果您要創建響應,那么這就是您代碼中所需要的:
var twilio = require('twilio');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.