fs.rename回调执行了两次?

时间:2014-05-15 16:04:50

标签: node.js

我遇到了fs.rename的问题。回调执行两次。看到这段代码:

log.d(_.keys(uploaded_photos));
async.each(_.keys(uploaded_photos), function(photo_key, callback) {
  var filename_array = uploaded_photos[photo_key].file.split('/');
  var filename = filename_array[filename_array.length-1];
  var tmp_file = __dirname + "/../public"+uploaded_photos[photo_key].file;
  log.d('Before readFile');
  async.waterfall([function(cb) {
    log.d('Inside waterfall');
    fs.rename(tmp_file, __dirname+"/../public/uploads/album_15_years/"+filename, function(err) {
      log.d('Moved file');
      cb(err);
    });
  }], function(err) {
    if(err) {
      log.e(err);
    } else {
      callback();
    }
  });
}, function() {
  var reply = {msg: "Answers and files saved successfully."};
  log.i(reply);
  res.send(201, reply);
});

这段代码输出:

[2014-05-15 11:51:55.932] [DEBUG] API.save_photos- ['photo1']
[2014-05-15 11:51:55.933] [DEBUG] API.save_photos- Before readFile
[2014-05-15 11:51:55.934] [DEBUG] API.save_photos- Moved file
[2014-05-15 11:51:55.935] [INFO] API.save_photos- { msg: 'Answers and files saved successfully.' }
[2014-05-15 11:51:55.936] [DEBUG] API.save_photos- Inside waterfall
[2014-05-15 11:51:55.937] [DEBUG] API.save_photos- Moved file
[2014-05-15 11:51:55.937] [ERROR] API.save_photos- { [Error: ENOENT, rename '/home/sbolduc/www/dashboard/routes/../public/uploads/tmp/1400169113201-0.01.PNG']
  errno: 34,
  code: 'ENOENT',
  path: '/home/sbolduc/www/dashboard/routes/../public/uploads/tmp/1400169113201-0.01.PNG' }
Error: ENOENT, rename '/home/sbolduc/www/dashboard/routes/../public/uploads/tmp/1400169113201-0.01.PNG'
POST /api/form201 52ms - 52b

如您所见,async.each中的所有内容只被调用一次。与记录 Inside waterfall 的日志相同。但fs.rename内的所有内容都被调用了两次......

我尝试使用fs.readfile代替fs.rename(并将文件写入新文件夹并取消旧文件夹链接),但fs.readfile也会被调用两次......

你知道发生了什么吗?

0 个答案:

没有答案