JavaScript, Node.JS : unable to read and write a text file (async, await problem)

I am building a function that edits a PHP file using fs.readFile and fs.writeFile, but the PHP file stays the same. However, when I run the logic at the main function of the node.js file, it does edit the PHP file. The following is the code of my function.

create_page = async (event) => {
    // edits password.php
    const pwd_pg_loc = '/password.php'

    fs.readFile(pwd_pg_loc, 'utf8', async function (err, data) {
        if (err) return console.log(err)
        const result = await data.replace('$password = \`\`;', '$password= \'1234\';')
        fs.writeFile(pwd_pg_loc, result, 'utf8', function (err) {
            if (err) return console.log(err)
        })
    })
})

I think I have async, await problems here. Any advise is appreciated!



Read more here: https://stackoverflow.com/questions/68479421/javascript-node-js-unable-to-read-and-write-a-text-file-async-await-problem

Content Attribution

This content was originally published by ambb at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: