繁体   English   中英

从 Nodejs 传递错误但没有收到正确的错误

[英]Passing error from Nodejs but not receiving the right one

这是我的代码,当扩展不是 mp4 时,我正在获取上传视频并检查它的扩展名我想返回 else 语句并从服务器传递错误但是当我收到此错误时以及当我在控制台时记录此错误打印服务器没有响应 505 错误,当扩展为 mp4 时它可以正常工作。

const videoStorage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, config.videoStorage);
  },
  filename: function (req, videoFile, cb) {
    if (path.extension(videoFile.originalname) !== '.mp4') {
      const name = `${videoFile
        .fieldname}-${Date
        .now()}_${
      videoFile.originalname}`;
      return cb(null, name.replace(/\s/g, ""));
    } else {
      return cb(new Error("sorry"));
    }
  }
});

MediaService
  .uploadVideo(formData, this.getUploadProgress)
  .then(data => {
    let order = {
      ...this.state.project
    };
    if (!order.project) {
      order = {
        project: {
          mediaId: data
        }
      };
    }
    order.project.mediaId = data;
    console.log("videoid added ===", order);
    this.setState({uploading: false, videoId: data, isValid: true, project: order});

    message.success("Video uploaded successfully");
  })
  .catch(error => {
    message.error(error);
    this.setState({message: error, uploading: false});
  });
};

您可以像这样传递标志,而不是抛出错误:

return cb({success:false,message:"false"});

您可以访问上传者的成功承诺。

因为当你说new Error() ,我认为它会在服务器端抛出一个错误并且它无法向客户端发送响应。这就是你没有在客户端得到响应的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM