Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么赢了';t writeFile work node.js_Node.js - Fatal编程技术网

为什么赢了';t writeFile work node.js

为什么赢了';t writeFile work node.js,node.js,Node.js,为什么对fs.writeFile的调用不起作用?我在Linux Ubuntu上。调用在这个gulp任务的最后,我尝试在文件上写一个字符串,我已经验证了这个字符串是正确的。清单[key]代码正在返回正确的文件名。除非我错过了一些愚蠢的东西,否则它应该会起作用。请帮忙!writefile调用未显示错误。但是文件没有被重写 gulp.task('injecttags', function() { const manifest = jsonfile.readFileSync('./public/mani

为什么对fs.writeFile的调用不起作用?我在Linux Ubuntu上。调用在这个gulp任务的最后,我尝试在文件上写一个字符串,我已经验证了这个字符串是正确的。清单[key]代码正在返回正确的文件名。除非我错过了一些愚蠢的东西,否则它应该会起作用。请帮忙!writefile调用未显示错误。但是文件没有被重写

gulp.task('injecttags', function() {
const manifest = jsonfile.readFileSync('./public/manifest/manifest.json');

const htmlkeys = [];
Object.keys(manifest).forEach((key) => {
    if (key.endsWith('.html')) {
        htmlkeys.push(key);
    }
});

htmlkeys.forEach((key) => {
    console.log("For key:", key);
    let file_content = fs.readFileSync('./public/html/'+manifest[key]).toString();
    console.log(file_content);
    let position = 0;
    while (((position = file_content.indexOf('<!-- INSERTSCRIPT=', position)) !== -1)) {
        const actualpositiontowriteto = file_content.indexOf('\n', position);
        const openquotepos = file_content.indexOf("'", position) + 1;
        const closequotepos = file_content.indexOf("'", openquotepos);
        const keystr = file_content.substring(openquotepos, closequotepos);
        const toinsert = `<script src="${manifest[keystr]}"></script>\n`;
        const beg = file_content.substr(0, position);
        const end = file_content.substr(actualpositiontowriteto);
        file_content = beg + toinsert + end;
    }
    position = 0;
    while (((position = file_content.indexOf('<!-- INSERTCSS=', position)) !== -1)) {
        const actualpositiontowriteto = file_content.indexOf('\n', position);
        const openquotepos = file_content.indexOf("'", position) + 1;
        const closequotepos = file_content.indexOf("'", openquotepos);
        const keystr = file_content.substring(openquotepos, closequotepos);
        const toinsert = `<link rel="stylesheet" href="${manifest[keystr]}">\n`;
        const beg = file_content.substr(0, position);
        const end = file_content.substr(actualpositiontowriteto);
        file_content = beg + toinsert + end;
    }

    fs.writeFile('./public/html/'+manifest[key], file_content, (err) => {
        if (err) return console.log('error writing file!', err);
        return console.log('successful file write');
    });
});
});
gulp.task('injecttags',function(){
const manifest=jsonfile.readFileSync('./public/manifest/manifest.json');
常量htmlkeys=[];
Object.key(manifest.forEach((key)=>{
if(key.endsWith('.html')){
htmlkeys.push(按键);
}
});
htmlkeys.forEach((键)=>{
日志(“对于键:”,键);
让file_content=fs.readFileSync('./public/html/'+manifest[key]).toString();
console.log(文件内容);
设位置=0;

而(((position=file_content.indexOf)(““不起作用”并不是对问题的详细描述。您在控制台中看到了什么?您是否在尝试使用其结果之前等待写入完成。您进行了哪些调试?在调试过程中您学到了什么?是的,我只是在运行任务后在文本编辑器中简单地查看文件。似乎与写入之前的文件完全相同。如我所述,我已经验证了manifest[key]等各种输入是否正确。令人困惑的是,节点说写操作没有错误,但我看不到任何写操作。然后,它正在写的文件可能位于与您所看到的不同的位置。
fs.writeFile()
不会说谎。如果它没有报告错误,并且您确定正在等待它完成,那么它将成功写入。因此,可能是
/public/html/'+manifest[key]
并不是你想象的那样。我在Node的9.x.x版本上,现在我切换到了8.11.1,它是一样的。有时它会明显地写入到有问题的文件中,有时它不会。当我循环不好时,我会得到很多合法的写入,但输出不正确。我修复了循环,现在根本没有写入。这太奇怪了,哈哈。