[ad_1]
I’m trying to read an .html document using node, creating an array with said document, then attempting to iterate thru the array to look for placeholders that are in the document to swap data from an object that was created from a .csv read file.
When I console.log the array after running, it shows the information as though it’s never been edited with the forEach loop. Am I missing something with async code?
Here it is:
import * as fs from 'node:fs';
import { parse } from 'csv-parse';
fs.readFile('test.csv', 'utf8', (err, data) => {
let matrix = [];
if (err) throw err;
parse(data, {columns: true, trim: true}, function(err, rows) {
if (err) throw err;
rows.forEach(row => {
// console.log(row)
const newCode = []
for (let [key, value] of Object.entries(row)) {
// console.log(key)
const keyToSearch = `{(${key})}`
try {
const data = fs.readFileSync('index.html', 'utf8')
const dataArr = data.split('\n')
dataArr.forEach(line => {
if (!line.includes(keyToSearch)) {
newCode.push(line)
} else {
let newLine = line.replace(keyToSearch, value)
newCode.push(newLine)
}
})
}catch(err) {
console.error(err)
}
}
console.log(newCode)
})
})
})
[ad_2]