gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'sid' of undefined

转载 作者:搜寻专家 更新时间:2023-11-01 00:31:14 26 4
gpt4 key购买 nike

我正在尝试发送一条短信(在 Twilio 的 API Explorer 中有效),但在我的 Node 安装下似乎失败了。我刚刚完全卸载并重新安装,但无济于事。

错误

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 的回调用作

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

关于javascript - 类型错误 : Cannot read property 'sid' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32989972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com