admin管理员组文章数量:1391995
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.onplete (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.onplete (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 pare 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
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.onplete (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.onplete (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 pare 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
Share
Improve this question
edited Jan 10, 2019 at 8:29
Anand Vegaraju
asked Jan 10, 2019 at 8:11
Anand VegarajuAnand Vegaraju
271 gold badge1 silver badge7 bronze badges
5
-
We can't help you without an example of the JSON. From the error, clearly
json_value[i].Subject
isnull
, but we can't tell you why without the JSON. – T.J. Crowder Commented Jan 10, 2019 at 8:15 -
"I tried using other string functions as well but keep getting similar errors." because you are not operating on a string but
undefined
. That's what the error message is telling you when you dojson.value[i].Subject.match()
thejson.value[i].Subject
part evaluates toundefined
, which doesn't have a.match()
method. Examine your data and see if.Subject
is the correct property to use. Note that it's case sensitive. – VLAZ Commented Jan 10, 2019 at 8:15 - Thanks for your replies. The JSON is quite large. I am able to print json.value[i].Subject on console. – Anand Vegaraju Commented Jan 10, 2019 at 8:17
- The JSON may be large, but it's entirely possible to show us its structure and/or a trimmed-down version of it. See: minimal reproducible example – T.J. Crowder Commented Jan 10, 2019 at 8:25
- Hi TJ. I am able to run if I use a relational operator (==) to pare 'Subject' with a hard coded String (Updated the question). Thanks for your response. – Anand Vegaraju Commented Jan 10, 2019 at 8:31
1 Answer
Reset to default 3Change 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.
版权声明:本文标题:node.js - How to fix "Cannot read property 'match' of undefined" error in JavaScript? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744586683a2614238.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论