简体   繁体   English

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

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

Here is my code where I am getting the upload video and checking it extension when extention is not mp4 i want to return else statement and pass the the error from server but when i am getting this error and when i am console.log this error this print the server not responding 505 error it is working fine with when extention is mp4.这是我的代码,当扩展不是 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});
  });
};

Instead of throwing error,you can pass flag like this:您可以像这样传递标志,而不是抛出错误:

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

This you can access in success promise of uploader.您可以访问上传者的成功承诺。

Because when you are saying new Error() , i think it will throw an error on server side and it will not be able to send response to client.that is why you are not getting response on client side.因为当你说new Error() ,我认为它会在服务器端抛出一个错误并且它无法向客户端发送响应。这就是你没有在客户端得到响应的原因。

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

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