TypeError:无法读取未定义的属性“sid”
我试图发送一个短信(在Twilio的API资源pipe理器中工作),但似乎在我的节点安装下失败。 我刚刚完成卸载并重新安装没有用。
错误
7 Oct 21:28:37 - [nodemon] starting `node scraper.js` Free on Xbox One, Xbox 360, PS3, PS4: Tales from the Borderlands (Episode 1) /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:126 throw e; ^ TypeError: Cannot read property 'sid' of undefined at /Users/rhysedwards/Downloads/insightful/ozbargain/scraper.js:39:31 at /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:1924:17 at flush (/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:108:17) at doNTCallback0 (node.js:408:9) at process._tickCallback (node.js:337:13) 7 Oct 21:28:39 - [nodemon] app crashed - waiting for file changes before starting...
错误的剥离twilio代码;
7 Oct 22:24:44 - [nodemon] starting `node scraper.js` /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:126 throw e; ^ TypeError: Cannot read property 'sid' of undefined at /Users/rhysedwards/Downloads/insightful/ozbargain/scraper.js:12:24 at /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:1924:17 at flush (/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:108:17) at doNTCallback0 (node.js:408:9) at process._tickCallback (node.js:337:13) 7 Oct 22:24:46 - [nodemon] app crashed - waiting for file changes before starting...
码
var accountSid = 'AC*******'; var authToken = 'da********'; var fs = require('fs'), request = require('request'), cheerio = require('cheerio'), client = require('twilio')(accountSid, authToken); url = 'http://www.ozbargain.com.au'; request(url, function(error, response, html) { if (!error && response.statusCode == 200) { var $ = cheerio.load(html); var $el = $("a:contains('Xbox')"); if ($el.length) { client.messages.create({ to: "61448141065", from: "+61418739508", body: "hey", }, function(err, message) { console.log(message.sid); }); console.log($el.text()); } else { console.log('hey'); } } });
client.messages.create
的callback被用作
}, function(err, message) { console.log(message.sid); });
当出现错误时,callbackerr
的第一个参数将包含与错误有关的信息,而第二个参数message
将是undefined
。
更新代码如下来处理错误的情况
}, function (err, message) { if (err) { // Handle error // Show appropriate message to user } else { // No error if (message.sid) { // Use sid here } } console.log(message.sid); });
你可以简单的改变console.log(message.sid); 至
if(err) { console.log(err.message); }
Twilio开发者在这里传道。
你的代码在事物的两侧有几个错误。
client.messages.create({ to: "61448141065", from: "+61418739508", body: "hey", }, function (err, message) { console.log(message.sid); });
对于在电话号码之前缺less一个+号码,在主体之后,您需要删除一个额外的逗号。
你的最终代码应该如下所示:
var accountSid = 'AC*******'; var authToken = 'da********'; var fs = require('fs'), request = require('request'), cheerio = require('cheerio'), client = require('twilio')(accountSid, authToken); url = 'http://www.ozbargain.com.au'; request(url, function(error, response, html) { if (!error && response.statusCode == 200) { var $ = cheerio.load(html); var $el = $("a:contains('Xbox')"); if ($el.length) { client.messages.create({ to: "+61448141065", from: "+61418739508", body: "hey" }, function(err, message) { console.log(message.sid); }); console.log($el.text()); } else { console.log('hey'); } } });
我在这里testing它,并改变后,它工作得很好。
希望这可以帮助你。
更新:尝试更改您的代码只是这样的:
var accountSid = 'AC*******'; var authToken = 'da********'; var fs = require('fs'), client = require('twilio')(accountSid, authToken); client.messages.create({ to: "+61448141065", from: "+61418739508", body: "hey" }, function (err, message) { console.log(message.sid); });
这是因为你的通话失败,没有消息对象。 更换
console.log(message.sid);
同
if (err) console.log(err.message); if (message) console.log(message.sid);
然后你会看到错误信息。