Linkify best way to impliment/ function 'replace' error

Want to "linkafy" text thats being generated into an HTML from node-red
am getting an error from a function node that seems node-red related

function : (error)

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

function linkify(themessage) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = themessage.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = themessage.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links.
    replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}
msg.payload.message = linkify(msg.payload.message);
return msg;

May happen if incoming msg.payload has no message property.
Put debug BEFORE the function node to see what kind of input is feed to the function or use debugging options inside the function
node.warn('incoming message:'+msg.payload.message)

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.