簡體   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