replace the string from the html file that is inserted into the string variable

2944 views javascript

I have a code to convert a string into a new string that is derived from a variable

const htmlstring = fs.readFileSync(config.message.letter);

function replace_tags(input, email) {
    return input
    .replace("{randomip}", random.ip)
    .replace("{email}", email)

function get_customised_message_template(email) {
    return {
        subject: replace_tags(config.message.subject, email),
        fromname: replace_tags(config.message.fromname, email),
        fromemail: replace_tags(config.message.fromemail, email),
        html: replace_tags(htmlstring, email) // >> here the error

here I want to replace the string inside the html file that I enter into the htmlstring variable using readfilesync

sample in html file

<b>mati lu anjeng {email} {randomip}</b>

i need to replace the tag like the others using the replace_tags() function but i get this error

(node:4784) UnhandledPromiseRejectionWarning: TypeError: input.replace is not a function

what should I do to outsmart it

answered question

is random some global object?

It would be better if you can create a fiddle of your problem.

random is object in another file

can you copy whole code with place where you call that get_customised_message_template method?? Otherwise it s not clear how you use it. Other than this, I would say that you should pass all the variables you use inside a method as a parameters to that method. So your method will look like: get_customised_message_template(htmlString, email, config). Will also be easier to debug this way.

1 Answer


This might possibly be because .readFileSync returns a buffer and not a string and .replace method is a string method. so try to convert the read buffer into a utf8 encoded string as below

const htmlstring = fs.readFileSync(config.message.letter)

posted this

Have an answer?


Please login first before posting an answer.