"TypeError: Cannot read property 'match' of undefined"

I try to build an email scraper to get all tracking numbers.
I made a regex to filter out some of the email but I run in to this error that I can’t get around.

As I understand regex that fails gets null return, so why do I get this error message?

In other places where the regex fails I see this:
image
So why do I get the type error at the first regex?

Test if msg.topic is null. That’s why match is erroring: msg.topic is null for whatever reason (you’ll have to debug that further up in your flow).

I doubt that is the issue.
But I’ll have a look.

It’s not all messages that do not match that errors, but it’s a lot.
I doubt I have that many email with no topic.

I can’t see what the message is that made the error and since it’s running through all my emails it’s hard to know what email caused the error.

Can you post your flow? I might be able to help you debug that.

If “msg.topic” is not a string, then TypeScript is going to throw that exact error.

You could simply have:

("" + msg.topic).match(pattern)

and that way you are guaranteed to have a string before trying to call “match”

2 Likes

Sure.
I’ll do that as soon as I can.

I can try…
But why would topic not be a string.

Just to be clear msg.topic is what the email topic is.
“Your shipment is on its way”, or “xxx posted a new video on YouTube” or whatever it says.

It has to be string… what else could it be?
It can’t be boolean or array. Sure integer and float if the topic is “1.1 video has been posted” and the email node by some reason takes that as a float.

I’ll post the sequence soon. But I have to remove my email and such so you have to try on your one account and see if you can replicate it somehow.

Well, in your case, it could be a null if the email parser in node isn’t able to read the subject line. Sounds dumb, but it’s software and email… I learned a LONG time ago to never trust the source and check everything when writing code.

As for the flow, just post a screencap. I don’t need the exported flow (and thus your personal data). :slight_smile:

Sequence.

Don’t mind the mess… I will clean up in the function nodes and all that. I just trying to get something out of it as it is now.
But on the positive side, it works with two carriers :slight_smile:

[{"id":"df9e7d98.0ad43","type":"inject","z":"ebaa69a9.649708","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":460,"wires":[["310c26fe.f97bca","7edac5a0.2a0e3c"]]},{"id":"7edac5a0.2a0e3c","type":"e-mail in","z":"ebaa69a9.649708","name":"","protocol":"IMAP","server":"imap.one.com","useSSL":true,"port":"993","box":"INBOX","disposition":"None","criteria":"ALL","repeat":"300","fetch":"trigger","inputs":1,"x":230,"y":520,"wires":[["e695d4bd.06b358"]]},{"id":"e695d4bd.06b358","type":"function","z":"ebaa69a9.649708","name":"","func":"var d = new Date(); \nd.setMinutes(d.getMinutes() - 6);\n\nmsg.tid = Date.parse(msg.date);\nmsg.tidnu = d.getTime();\n\n/*if(parseInt(msg.tid) > msg.tidnu){\n    msg.message = \"start\";\n}else{\n    msg.message = \"stop\";\n}\n\nif(msg.from == \"[email protected]\"){\n    msg.message = \"stop\";\n}*/\nvar match =[];\nmatch = msg.topic.match(/paket på väg|paket från|spåra di|leverans/i);\nif(match !== null){\n    msg.message = \"start\";   \n}else{\n    msg.message = \"stop\";\n}\n/*\nif(msg.from  == \"[email protected]\"){\n    msg.message = \"start\";\n}else{\n    msg.message = \"stop\";\n}*/\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":440,"wires":[["e56641fe.45dae"]]},{"id":"e56641fe.45dae","type":"switch","z":"ebaa69a9.649708","name":"","property":"message","propertyType":"msg","rules":[{"t":"neq","v":"stop","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":530,"y":460,"wires":[["baf947f7.a62cc8"]]},{"id":"baf947f7.a62cc8","type":"function","z":"ebaa69a9.649708","name":"","func":"var match = [];\nif(msg.from == \"[email protected]\"){\n    match = msg.topic.match('Paket från Bring: (.*)')\n}else{\n    match = msg.html.match(/(\\d{20})/);\n    if(match === null){\n        match = msg.html.match(/\\d{11}\\w{2}/);\n    }\n}\nmsg.tracking = match;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":520,"wires":[["777caa3e.1ec134"]]},{"id":"777caa3e.1ec134","type":"debug","z":"ebaa69a9.649708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":480,"wires":[]}]

with “”+msg.topic it reduced the errors to 1 message that created a type error. That is good enough.
Now I can see the messages I want to see.

1 Like

I went ahead and tried it with my own flow:

As suspected, an email with no subject doesn’t create a msg.topic:

image

:+1:

1 Like