我已经尝试了许多解决方案(如异步等待,承诺等),我不能找到一些真正的工作。
我正在尝试做一个函数,替换一个文件内的文本,它的工作,但问题发生时,你试图替换同一个文件内的文本夫妇。
这就是函数:
function replaceInFile(tDir, orgTxt, txtTR) {
console.log(`Dir: ${tDir} | orgTxt{ ${orgTxt} } txtTR.length{ ${txtTR.length} }`);
fs.readFile(tDir, 'utf8', function (err, data) {
if (err) {
console.log(err);
}
console.log(`DATA.LENGTH BR: ${data.length}`);
let result = data.replace(orgTxt, txtTR);
fs.writeFile(tDir, result, 'utf8', function (err) {
console.log(`RESULT.LENGTH AW: ${result.length}`);
if (err) {
console.log(err);
}
});
});
}
另一个函式会以下列方式呼叫函式:
replaceInFile(destDir + 'word/header1.xml', /{{NAME}}/g, rabTxt + fullName);
replaceInFile(destDir + 'word/header1.xml', /{{DATE}}/g, `${day} ${month} ${year}`);
replaceInFile(destDir + 'word/document.xml', /{{HAXCABH}}/g, haxcaba);
replaceInFile(destDir + 'word/document.xml', /{{LETTERS}}/g, nameLetterSq(fullName));
replaceInFile(destDir + 'word/document.xml', /{{MRQY}}/g, isMrq());
我得到的输出与我尝试做什么无关:
ON: "renderer.js:1294" - Dir: ./temporal/word/header1.xml | orgTxt{ /{{NAME}}/g } txtTR.length{ 14 }
ON: "renderer.js:1294" - Dir: ./temporal/word/header1.xml | orgTxt{ /{{DATE}}/g } txtTR.length{ 13 }
ON: "renderer.js:1294" - Dir: ./temporal/word/document.xml | orgTxt{ /{{HAXCABH}}/g } txtTR.length{ 2811 }
ON: "renderer.js:1294" - Dir: ./temporal/word/document.xml | orgTxt{ /{{LETTERS}}/g } txtTR.length{ 15647 }
ON: "renderer.js:1294" - Dir: ./temporal/word/document.xml | orgTxt{ /{{MRQY}}/g } txtTR.length{ 6919 } (2 times)
ON: "renderer.js:1299" - DATA.LENGTH BR: 4803 (3 times)
ON: "renderer.js:1299" - DATA.LENGTH BR: 43388
ON: "renderer.js:1302" - RESULT.LENGTH AW: 4808
ON: "renderer.js:1302" - RESULT.LENGTH AW: 59024
ON: "renderer.js:1302" - RESULT.LENGTH AW: 50299
ON: "renderer.js:1302" - RESULT.LENGTH AW: 46188
ON: "renderer.js:1302" - RESULT.LENGTH AW: 4809
我希望函数一直运行到结束,因此所需的输出将如下所示(关注其顺序):
ON: "renderer.js:1294" - Dir: ./temporal/word/header1.xml | orgTxt{ /{{NAME}}/g } txtTR.length{ "Some Number" }
ON: "renderer.js:1299" - DATA.LENGTH BR: "Some Number"
ON: "renderer.js:1302" - RESULT.LENGTH AW: "Some Number"
ON: "renderer.js:1294" - Dir: ./temporal/word/header1.xml | orgTxt{ /{{DATE}}/g } txtTR.length{ "Some Number" }
ON: "renderer.js:1299" - DATA.LENGTH BR: "Some Number"
ON: "renderer.js:1302" - RESULT.LENGTH AW: "Some Number"
ON: "renderer.js:1294" - Dir: ./temporal/word/document.xml | orgTxt{ /{{HAXCABH}}/g } txtTR.length{ "Some Number" }
ON: "renderer.js:1299" - DATA.LENGTH BR: "Some Number"
ON: "renderer.js:1302" - RESULT.LENGTH AW: "Some Number"
ON: "renderer.js:1294" - Dir: ./temporal/word/document.xml | orgTxt{ /{{LETTERS}}/g } txtTR.length{ "Some Number" }
ON: "renderer.js:1299" - DATA.LENGTH BR: "Some Number"
ON: "renderer.js:1302" - RESULT.LENGTH AW: "Some Number"
ON: "renderer.js:1294" - Dir: ./temporal/word/document.xml | orgTxt{ /{{MRQY}}/g } txtTR.length{ "Some Number" }
ON: "renderer.js:1299" - DATA.LENGTH BR: "Some Number"
ON: "renderer.js:1302" - RESULT.LENGTH AW: "Some Number"
平台为“Electron”,代码位于“renderer.js”默认文件中。
谢谢你,谢谢
1条答案
按热度按时间ncecgwcz1#
一种解决方案是将函数转换为promise,并在编写文件后解析
promise
。