I'm new to Node JS and am trying to set up a basic application that fetches my inbox (outlook) contents in JSON format. I tried using match function to filter the data based on subject but I keep getting this error -
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)
I tried using other string functions as well but keep getting similar errors.
I am able to print 'json.value[i].Subject' in console but cannot use it in match function.
/ Request portion /
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)
I expect the match function to work so that I can filter based on Subject (json.value[i].Subject) but I guess I missed out something.
However I am able to get results if I hard code some string to compare with the 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
Change your if in:
const subject = json.value[i].Subject; // to make it shorter
if(subject && subject.match(/Action Required/)) {
// your code
}
Using this if condition you'll use .match()
function only if json.value[i].Subject
is defined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.