我遇到了一个特定于我的Reaction Native EXPO开发版本的问题.我的应用程序中有多个API,虽然它们中的大多数都可以正常工作,但在try 向某个特定的API发出请求时,我总是得到100,但有一个特定的API在Postman中工作得很好.

我只在Android Real设备上运行我的项目

以下是有关这个问题的一些细节:

  • 环境:react 本土expo 发展建设

  • 问题:只出现在一个特定的API上,其他API工作正常

  • 错误消息:100

  • 接口:http://myIP:8080/api/v1/post/Deal-Posts

  • 请求方式:[POST]

console error个 这是我的控制台错误图片

this below is my network header
enter image description here

这是我的代码

const handleDealPost = async () => {
    try {
      const titleInFilename = title.replace(/\s+/g, "_");
      const currentDate = new Date();
      const formattedDate = currentDate.toLocaleDateString().replace(/-/g, "");
      const imageKey = `${titleInFilename}_${formattedDate}.jpg`;
      const formData = new FormData();
      formData.append("dealURL", dealURL);
      formData.append("title", title);
      formData.append("company", company);
      formData.append("discountedPrice", discountedPrice);
      formData.append("originalPrice", originalPrice);
      formData.append("description", description);
      formData.append("category", category);
      formData.append("dealType", dealType);
      formData.append("image", {
        uri: image.uri,
        type: image.type,
        name: imageKey,
      });
      formData.append("expiryDate", expiryDate.toString());

      // console.log("FormData:", formData);

      const response = await axios.post(
        "http://MyIp:8080/api/v1/post/deal-posts",
        formData,
        {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        }
      );
      const result = response.data;
      if (response.status === 201) {
        Alert.alert("Success", result.message);
        setPosts([...posts, result?.post]);
        // navigation.navigate("BottomTabsNavigator");
        navigation.navigate("Home");
        setDealURL("");
        setTitle("");
        setCompany("");
        setDiscountedPrice("");
        setOriginalPrice("");
        setDescription("");
        setCategory(null);
        setDealType(null);
        setImage(null);
        setExpiryDate("");
      } else {
        Alert.alert("Error", result.message);
      }
    } catch (error) {
      console.error("Error:", error);
      Alert.alert("Error", "An error occurred while posting the deal");
    }
  };
  • 选中的API终结点

  • 使用Postman等工具测试API端点[工作正常]

  • 我在相同的开发版本中测试了其他API,它们工作起来没有任何问题.

推荐答案

添加类型:在formData的值中

例如:

const formData = new FormData();

formData.append("myImage", {
    uri: "myFileURI",
    name: "myfileName",
    **type: "image/jpeg"**, // 🎉 Add this!!!!!!!
})

React-native相关问答推荐

1.5.1 nxworkspace+react-native 版本的Axios url未定义问题

无法读取 @react-native-async-storage/async-storage 的未定义属性setItem

将 Cognito 用户与真实数据库用户联系起来的最佳方式是什么?

React Native:如何以编程方式对图像进行黑白过滤?

React Native Child Parent 通信

判断状态对象是否为空

React Native:在 ScrollView contentContainerStyle 上设置 flex:1 会导致组件重叠

react-native-firebase crashlytics 未显示在 firebase 仪表板上

我如何知道我的代码是否作为 React Native 运行

react-native app.js index.js

React Native Card Carousel 视图?

library not found for -lRCTGeolocation

这个 refs 在方法中变得未定义

使用 create-react-native-app 创建了一个应用程序,如何将其发布到 Google Play store ?

如何从 stack.navigation 外部的组件中使用 navigation.navigate

使用 React Navigation 制作离开屏幕的动画

将图标添加到drawer抽屉react-navigation v5

如何隐藏 React Native NavigationBar

更改导航标题背景 colored颜色

如何从react-native应用程序打开外部应用程序?