2015-10-07 162 views
1

我试图发送短信(在Twilio的API Explorer中工作),但似乎在我的节点安装下失败。我刚刚完成卸载并重新安装无效。TypeError:无法读取未定义的属性'sid'

错误

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'); 
     } 
    } 
}); 

回答

2

client.messages.create回调用作

}, function(err, message) { 
    console.log(message.sid); 
}); 

当会有误差,则回调err的第一参数将包含与错误相关的信息,第二个参数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); 
}); 
+0

对不起,没有解决问题。 Nodemon仍然会崩溃'TypeError:无法读取未定义的属性'sid'。我相信问题必须是节点如何访问Twilio节点模块。 –

+0

@RhysEdwards您是否在代码中的任何其他位置使用过'.sid',如果有的话,也在那里添加类似的代码。另外,检查'message'中是否有响应。 – Tushar

+0

这就是我写的代码源的全部内容(显然依赖于调用的模块)。没有从消息中得到任何答复。假设这是由于TypeError的崩溃。 –

0

Twilio开发商传道这里处理错误条件。

你的代码在事物的twilio一侧有几个错误。

client.messages.create({ 
    to: "61448141065", 
    from: "+61418739508", 
    body: "hey", 
}, function (err, message) { 
    console.log(message.sid); 
}); 

To你错过了之前的电话号码的+和人体后,你有你需要删除多余的逗号。

您的最终代码看起来应该像下面这样:

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'); 
     } 
    } 
}); 

我在这里测试,它改变的是以后工作的罚款。

希望这可以帮助你。

UPDATE: 试着改变你的代码,只是这样的:

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); 
}); 
+0

好用的逗号和加号。不幸的是,它仍然与sid TypeError失败;开始认为这可能是一个有趣的节点安装,但无法找到它。 –

+0

@RhysEdwards看看我的更新。这必须肯定有效。 –

+0

嘿@marcosplancona,感谢帮助。给它一个去,它仍然失败。这是一个很奇怪的问题,这就是为什么我干净地安装node/npm。也许这是一个新的问题节点v4.1.2?我已将最新的错误转储添加到OP。 –

2

你可能只是更改执行console.log(message.sid); 至

if(err) { 
    console.log(err.message); 
} 
+0

欢迎来到StackOverflow!请扩展您的答案并解释为什么这可以解决问题。就像现在一样,它更像是一条评论,如果以回答的形式被删除,那么它们会很快被删除 –

0

这是因为您的呼叫失败并且没有消息对象。更换

console.log(message.sid); 

if (err) console.log(err.message); 
if (message) console.log(message.sid); 

然后你会看到错误消息。

相关问题