2015-10-07 30 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); 

然後你會看到錯誤消息。

相關問題