skip to Main Content

Hi I am using axios to post my string and some buffer data in my mongodb server.Data is working fine with post but when I try to post data using axios it does show on console but it doesnot post data in mongo db here is my axios post code

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: "",
    });
  });




and here is the console data

Object {
"code": undefined,
"columnNumber": undefined,
"config": Object {
"adapter": [Function xhrAdapter],
"data": "{"_parts":[["name","Saad"],["title","Pets"],["contact","12345678900"],["price","123"],["description","Post"],["selectedcat","Pets Accessories"],["selectedcity","Karachi"],["imgforsell",{"cancelled":false,"width":2160,"exif":{"DateTime":"2015:11:04 17:32:48","Software":"Adobe Photoshop CC (Windows)","XResolution":72,"ImageWidth":2160,"Orientation":0,"ImageLength":1620,"ResolutionUnit":2,"LightSource":0,"ColorSpace":1,"JPEGInterchangeFormat":302,"YResolution":72,"Compression":6,"JPEGInterchangeFormatLength":5674},"height":1620,"base64":"/9j/4QG+RXhpZgAATU0AKgAAAAgADAESAAQAAAABAAAAAAEAAAQAAAABAAAIcAEaAAUAAAABAAAAngExAAIAAAAdAAAApgICF/nat node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:388:6 in __callImmediatesat node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:132:6 in __guard$argument_0at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:365:10 in __guardat node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:131:4 in flushedQueue

update.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: "",
    });
  });

2

Answers


  1. Have you ever checked your backend. Maybe you didn’t commit the changes. You need to debug in your backend.

    Login or Signup to reply.
  2. I’ve deleted this part:

    {
    headers: {
    ‘Content-Type’: ‘application/json’,
    }

    and got response from server

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search