Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';sid';未定义的_Javascript_Node.js_Twilio - Fatal编程技术网

Javascript TypeError:无法读取属性';sid';未定义的

Javascript TypeError:无法读取属性';sid';未定义的,javascript,node.js,twilio,Javascript,Node.js,Twilio,我试图发送一条短信(在Twilio的API浏览器中工作),但在我的节点安装下似乎失败了。我刚刚完成了一个完整的卸载并重新安装,但没有任何效果 错误 回退斜纹编码错误 7 Oct 22:24:44 - [nodemon] starting `node scraper.js` /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:126

我试图发送一条短信(在Twilio的API浏览器中工作),但在我的节点安装下似乎失败了。我刚刚完成了一个完整的卸载并重新安装,但没有任何效果

错误

回退斜纹编码错误

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...
代码


client.messages.create的回调用作

}, function(err, message) {
    console.log(message.sid);
});
出现错误时,回调的第一个参数
err
将包含与错误相关的信息,第二个参数
message
未定义

按如下方式更新代码以处理错误情况

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

这里是Twilio开发者福音传道者

在你的代码中,有一些事情是错误的

client.messages.create({
    to: "61448141065",
    from: "+61418739508",
    body: "hey",
}, function (err, message) {
    console.log(message.sid);
});
上,您在电话号码前缺少一个+号,在正文后,您需要删除一个额外的逗号

您的最终代码应该如下所示:

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');
        }
    }
});
我在这里测试了它,在改变它之后,它工作得很好

希望这对你有帮助

更新: 尝试将您的代码更改为:

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); 到


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

console.log(message.sid);


然后,您将看到错误消息。

很抱歉,这没有解决问题。Nodemon仍然崩溃,出现
TypeError:无法读取未定义的属性“sid”
。我认为问题一定是node如何访问Twilio node模块。@RhysEdwards您是否在代码中的任何其他地方使用了
.sid
,如果是,请在那里添加类似的代码。另外,检查
消息中的响应是否是我编写的全部代码源(显然依赖于调用的模块)。没有从消息中得到任何响应。假设这是由于TypeError崩溃造成的。您是否尝试将
err
记录在条件日志中
if(err){console.log(err);}
。那么你得到了什么?很好地理解了逗号和加号。不幸的是,它仍然失败,出现了sid-TypeError;开始认为这可能是一个有趣的节点安装,但找不到它。@请查看我的更新。这肯定有用。嘿@marcosplancona,谢谢你的帮助。试一试,它仍然失败。这是一个非常奇怪的问题,这就是为什么我干净地安装了node/npm。也许这是一个新的问题节点v4.1.2?我已将最新的错误转储添加到OP。欢迎使用StackOverflow!请扩展您的答案,并解释为什么这可以解决问题。现在,它更像是一条评论,如果以poset作为答案,这些评论会很快被删除
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);
});
if(err) {
    console.log(err.message);
}
console.log(message.sid);
if (err) console.log(err.message);
if (message) console.log(message.sid);