如何修复JavaScript中的“无法读取未定义的属性'匹配'”错误?

时间:2019-01-10 08:11:26

标签: javascript node.js json string

我是Node JS的新手,正在尝试建立一个基本应用程序,该应用程序以JSON格式获取我的收件箱(外观)内容。我尝试使用匹配功能根据主题过滤数据,但我一直收到此错误-

TypeError: Cannot read property 'match' of null
    at WriteStream.<anonymous> (C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE\OFFICE365_NJS_DEMO.js:44:29)
    at Object.onceWrapper (events.js:273:13)
    at WriteStream.emit (events.js:182:13)
    at lazyFs.open (internal/fs/streams.js:278:10)
    at FSReqWrap.oncomplete (fs.js:141:20)

我也尝试过使用其他字符串函数,但不断出现类似错误。

我可以在控制台中打印'json.value [i] .Subject”,但不能在匹配功能中使用它。

/ 请求部分 /

request(options, function(err, res, body) {  
let json = JSON.parse(body);

console.log(err);

var fileName = 'abc.html';
var html = '';
var stream = fs.createWriteStream(fileName);


stream.once('open', function(fd) {
for (var i = 0; i < json.value.length-1; i++){





        if(json.value[i].Subject.match(/Action Required/)){

            var dom = new JSDOM(json.value[i].Body.Content);

            console.log(json.value[i].Subject);

            html = html + '<br/>' + json.value[i].Subject + '<a href=' + dom.window.document.querySelector("a") + 'target="_top">Approve</a><br/><br/>';

        }       
}
html = '<!DOCTYPE html>'
   + '<html><head>' + "List Of Pending Items" + '</head><body>' + html + '</body></html>';
stream.end(html);
});

});

C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE>node OFFICE365_NJS_DEMO.js
null
FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-03-26 to 2019-03-26
FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-05-08 to 2019-05-08
C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE\OFFICE365_NJS_DEMO.js:44
                        if(json.value[i].Subject.match(/Action Required/)){
                                                 ^

TypeError: Cannot read property 'match' of null
    at WriteStream.<anonymous> (C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE\OFFICE365_NJS_DEMO.js:44:29)
    at Object.onceWrapper (events.js:273:13)
    at WriteStream.emit (events.js:182:13)
    at lazyFs.open (internal/fs/streams.js:278:10)
    at FSReqWrap.oncomplete (fs.js:141:20)

我希望match函数起作用,以便我可以基于Subject(json.value [i] .Subject)进行过滤,但是我想我错过了一些东西。

但是,如果我对某些字符串进行硬编码以与Subject进行比较,我就能得到结果。

if(json.value[i].Subject == 'FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-03-26 to 2019-03-26'){

                var dom = new JSDOM(json.value[i].Body.Content);

                console.log(json.value[i].Subject);

                html = html + '<br/>' + json.value[i].Subject + '<a href=' + dom.window.document.querySelector("a") + 'target="_top">Approve</a><br/><br/>';

            }

C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE>node OFFICE365_NJS_DEMO.js
null
FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-03-26 to 2019-03-26

1 个答案:

答案 0 :(得分:2)

在以下位置更改您的条件:

const subject = json.value[i].Subject; // to make it shorter

if(subject && subject.match(/Action Required/)) {
  // your code
}

仅当定义了.match()时,才使用json.value[i].Subject函数。