sails skipper:如何检查文件是否已上传

Man*_*ngh 2 node.js sails.js

我使用风帆0.10.5,使用船长上传文件.现在我有一个用户可以上传文件的表单.但是,文件上载不是必需的.在后端,我需要检查文件是否已上传.

我的代码是这样的:

if(req.file('img')){
  req.file('img').upload({
    maxBytes: 10000000
  }, function (err, uploadedFiles){
    if(err){
      return res.json(500, err);
    }
    else if(uploadedFiles.length === 0){
      return res.json(500, {"error": "no file uploaded"});
    }
    else{
        // do something with image
    }
  });
}
else {
    // proceed without image
}
Run Code Online (Sandbox Code Playgroud)

没有上传图片时出现以下错误:

Error: EMAXBUFFER: An Upstream (`NOOP_img`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.
    at null.<anonymous> (/home/mandeep/projects/thirstt/node_modules/sails/node_modules/skipper/standalone/Upstream/Upstream.js:86:15)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
Run Code Online (Sandbox Code Playgroud)

第一个if条件总是被评估为true.如何检查用户是否未上传任何文件?

Man*_*ngh 5

将我的代码更改为

req.file('img').upload({
  maxBytes: 10000000
}, function (err, uploadedFiles){
  if(err){
    return res.json(500, err);
  }
  else if(uploadedFiles.length === 0){
    // proceed without files
  }
  else{
    //  handle uploaded file
  }
});
Run Code Online (Sandbox Code Playgroud)

现在它的工作正常