【问题标题】:Passing error from Nodejs but not receiving the right one从 Nodejs 传递错误但没有收到正确的错误
【发布时间】:2020-05-07 20:13:35
【问题描述】:

这是我的代码,我在其中获取上传视频并在扩展不是 mp4 时检查它的扩展我想返回 else 语句并从服务器传递错误但是当我收到此错误并且当我是 console.log 时此错误会打印服务器没有响应 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});
  });
};

【问题讨论】:

    标签: node.js reactjs server promise react-redux


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2016-11-08
      • 2022-01-23
      • 2022-08-24
      • 1970-01-01
      相关资源
      最近更新 更多