0
votes

I'm getting a network error when trying to send a post request using axios.

Error: Network Error at node_modules\axios\lib\core\createError.js:15:17 in createError at node_modules\axios\lib\adapters\xhr.js:81:22 in handleError at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:600:10 in setReadyState at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0 at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue at [native code]:null in callFunctionReturnFlushedQueue

Code:

const [state, setTest] = useState({data: null});
  const[isLoading, setLoading] = useState(true);
  const {testowy} = require("./clientRequests/Creq_lib");
  // testowy().then(data => setState({data: data})).catch(error => {console.log(error)}); This one doesn't work
  useEffect(() => {
    // axios.post("http://192.168.0.4:8080/testowy").then(res => {setTest({data: res.data}); setLoading(false);});  This works ok
    // testowy().then(res => {setTest({data: res}); setLoading(false);});
    testowy().then(res => {setTest({data: res}); setLoading(false);}); // These two don't work
  }, []);

When using directly axios.post() everything works fine, but after i try to import a function, which should do the exact same thing i get this network error.

Library with the function mentioned above:

const {Creq_testowy} = require("./Creq_testowy");

module.exports={
    testowy: Creq_testowy,
}

Function itself:

const axios = require("axios");

function Creq_testowy() {
    return new Promise((resolve, reject) => {
        axios.post(`http://192.168.0.4:8080/testowy`)
            .then(res => {
                console.log(res.data);
                resolve(res.data);
            })
            .catch(error => {console.log(error)})
    });
}


module.exports = {
    Creq_testowy,
}

Why everything works ok when using axios.post() directly? Why i'm getting this error when using the imported function?

2

2 Answers

1
votes

why create a promise when axios already returns one ? can you try this ?

async function Creq_testowy() {
  const res = await axios.post(`http://192.168.0.4:8080/testowy`)
  return res.data;
}

or this

async function Creq_testowy() {
  try {
    const res = await axios.post(`http://192.168.0.4:8080/testowy`)
    return res.data;
  } catch(err) {
    console.log(err);
  }
}

are you running this code in reat native ? expo ? web ?

do you also pass post parameters that you have hiden for brevety ? it may be the root cause of the bug.

1
votes

Can you try replacing your code with following:

const axios = require("axios");

async function Creq_testowy() {
    const data = await axios.post(`http://192.168.0.4:8080/testowy`)
    return data.data
}

module.exports = {
    Creq_testowy,
}