axios 在控制台中显示数据,但未在 mongo db 中发布

问题描述 投票:0回答:2

嗨,我正在使用 axios 在我的 mongodb 服务器中发布我的字符串和一些缓冲区数据。数据在发布时工作正常,但是当我尝试使用 axios 发布数据时,它确实显示在控制台上,但它不会在 mongo db 中发布数据,这是我的axios 邮政编码

axios.js

const formData = new FormData();
      formData.append("name", this.state.name);
      formData.append("title", this.state.title);
      formData.append("contact", this.state.contact);
      formData.append("price", this.state.price);
      formData.append("description", this.state.description);
      formData.append("selectedcat", this.state.selectedcat);
      formData.append("selectedcity", this.state.selectedcity);

      formData.append("imgforsell", this.state.imgforsell);

axios
  .post(
   // `http://${
     // Platform.OS === "android" ? "192.168.88.45" : "localhost"
    //}:4000/pets/addpets`,
  
    'http://http://192.168.88.45:4000/pets/addpets',
    formData,
    {headers: { "Content-Type": "multipart/form-data" }}

    
  )
  .then(({ data }) => {
    console.log(data);
  })
  .catch((err) => {
    console.error(err.toJSON());
    // res.status(500).json(err) 👈 don't do this, it's not Express
  })
  .finally(() => {
    this.setState({
      name: "",
      title: "",
      contact: "",
      price: "",
      description: "",
      selectedcat: "",
      selectedcity: "",
      imgforsell: "",
    });
  });




这是控制台数据

对象{ “代码”:未定义, “列号”:未定义, “配置”:对象{ “适配器”:[函数xhrAdapter], "data": "{"_parts":[["姓名","萨阿德"],["头衔","宠物"],["联系方式","12345678900"],["价格","123"] ,["描述","帖子"],["selectedcat","宠物配件"],["selectedcity","卡拉奇"],["imgforsell",{"取消":false,"宽度":2160, "exif":{"DateTime":"2015:11:04 17:32:48","软件":"Adobe Photoshop CC (Windows)","XResolution":72,"ImageWidth":2160,"方向" :0,“图像长度”:1620,“分辨率单位”:2,“光源”:0,“色彩空间”:1,“JPEGInterchangeFormat”:302,“Y分辨率”:72,“压缩”:6,“JPEGInterchangeFormatLength”:5674 },“高度”:1620,“base64”:“/9j/4QG+RXhpZgAATU0AKgAAAAgADAESAAQAAAABAAAAAAAEAAAQAAAABAAAIcAEaAAUAAAABAAAAAngExAIAAAAdAAAApgICF/nat node_modules eact-native\Libraries\BatchedBridge\MessageQueue.js:388:6 在 __callImmediatesat node_modules 中 eact-native\Libraries\BatchedBridge\MessageQueue.js:132:6 在 __guard$argument_0at node_modules __guardat node_modules 中的 eact-native\Libraries\BatchedBridge\MessageQueue.js:365:10 FlushedQueue 中的 eact-native\Libraries\BatchedBridge\MessageQueue.js:131:4

更新.js


 const petsObject = {
          name: this.state.name,
          title: this.state.title,
          contact: this.state.contact,
          price: this.state.price,
          description: this.state.description,
          selectedcat:this.state.selectedcat,
          imgforsell:this.state.imgforsell
          
        };
        

    axios
  .post(
   // `http://${
    //  Platform.OS === "android" ? "192.168.88.45" : "localhost"
   // }:4000/pets/addpets`,
  
    //'http://192.168.88.45:4000/pets/addpets/',
    'http://localhost:4000/Pets/addpets/',
   // formData,
petsObject,
   
   
    {
      headers: {
        'Content-Type': 'application/json',
      },
      transformRequest: (formData,headers) => {
       return formData; // this is doing the trick
      },
    },
  
    
  )
  .then(({ data }) => {
    console.log(data);
   
   // .then(res => {
     // console.log('Data Posted',res);
      
   // }


  }
  )
  .catch((err) => {
    console.error(err.toJSON());
    
    // res.status(500).json(err) 👈 don't do this, it's not Express
  })
  .finally(() => {
    this.setState({
      name: "",
      title: "",
      contact: "",
      price: "",
      description: "",
      selectedcat: "",
      selectedcity: "",
      imgforsell: "",
    });
  });
mongodb react-native post axios expo
2个回答
0
投票

您检查过您的后端吗?也许您没有提交更改。您需要在后端进行调试。


0
投票

我已经删除了这部分:

{ 标题:{ '内容类型':'应用程序/json', }

并收到服务器的响应

© www.soinside.com 2019 - 2024. All rights reserved.