Reactjs 如何在react中为formData创建if-else?

Reactjs 如何在react中为formData创建if-else?,reactjs,form-data,Reactjs,Form Data,我正在尝试实现,如果用户不需要添加头像,但仍然创建一个团队,但我有一个问题,即如果用户不在表单中添加头像,它将导致一个错误,说文件:未定义 以下是我的handleSubmit代码: const handleSubmit = async (event) => { event.preventDefault(); // upload image to server const formData = new FormData(); formData.appen

我正在尝试实现,如果用户不需要添加头像,但仍然创建一个团队,但我有一个问题,即如果用户不在表单中添加头像,它将导致一个错误,说
文件:未定义

以下是我的handleSubmit代码:

  const handleSubmit = async (event) => {
    event.preventDefault();

    // upload image to server
    const formData = new FormData();
    formData.append("file", imageSelected.raw);

    const pictureUrl = await axios.post(
      "/api/v1/teams/upload/image",
      formData,
      {
        headers: { "Content-Type": "multipart/form-data" },
      }
    );

    const teamAvatar = pictureUrl.data.data.imageUrl;

    // after upload image to server get image data and send it to create team function
    const payload = { title, mission, isSearchable, teamAvatar };
    // Send post request for signup
    const res = await axios.post("/api/v1/teams/team", payload);

    // If no validation errors were found
    if (res.data.validationErrors === undefined) {
      // Clear any errors
      setErrorsArr([]);
      // Hide the errors component
      setShowErrors(false);
      // Toggle the modal
      toggleModal();
      // Go to team management page
      NextRouter.push(`/team/${res.data}`);
      // console.log("payload", payload);
    } else {
      // Set errors
      setErrorsArr(res.data.validationErrors.errors);
      // Show the errors component
      setShowErrors(true);
    }
  };
如何为formData创建if-else语句,以帮助用户不需要添加化身,但仍然可以创建团队使用

if(imageSelected){
 formData.append("file", imageSelected.raw);
}