相关疑难解决方法(0)

在Nodejs中编写多个文件循环

嗨我在nodejs脚本中使用每个循环来将多个文件写入本地位置.对于courseTitleArray我正在使用"Biology 101,ruby",我可以成功写入一个文件,但不能同时两个.请帮助我出.

到目前为止,这是我的代码

  for (var value in CourseTitleArray) {
               console.log( "Course Title " + CourseTitleArray[value]);
               var newImageLocation = path.join(__dirname, 'app/img/courseImages', CourseTitleArray[value] +  ".png");

                  fs.readFile(image.path, function(err, data) {
                      fs.writeFile(newImageLocation, data, function(err) {
                      console.log(CourseTitleArray[value] + " was  created successfully");   




                      });
                  }); 

                console.log("Loop executed " +  value);  
             }
Run Code Online (Sandbox Code Playgroud)

在控制台中我得到以下日志.

Course Title Biology 101
Loop executed 0
Course Title ruby
Loop executed 1
ruby was  created successfully
ruby was  created successfully 
Run Code Online (Sandbox Code Playgroud)

似乎循环工作正常,在日志中我可以看到两个标题.但当它写"生物学101,红宝石"已经执行了两次.

有人可以帮我解决这个问题吗?谢谢

javascript fs node.js

2
推荐指数
1
解决办法
2301
查看次数

标签 统计

fs ×1

javascript ×1

node.js ×1