简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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