question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble with axios ( pending all the time )

See original GitHub issue

Hello. I have trouble with axios. I used axios in order to send data on server. req.body is send correctly and app almost work but there is a problem. axios is pending in network some long time and after that activate catch() instead of then() . I will be grateful if someone spend little of his time and help with this. 😃 (MERN app)

(frontend)

import React, {Component} from 'react';
import axios from 'axios';
class App extends Component {
 state={
   title:"",
   body:"",
   post:[]
 }
handleChange=(e)=>{
  this.setState({
    [e.target.name]:e.target.value,
  })
  
}
handleSubmit=(e)=>{
  e.preventDefault()
  const payload ={
    title:this.state.title,
    body: this.state.body
  }
  axios({
    url:"/api/save",
    method:"POST",
    data:payload,
   
  })
  .then(()=>{
    console.log("wysłane poprawnie")
  })
  .catch((err)=>{
     console.log(err)
  })
}


  render() { 
    return ( 
<>

<h1>Witam na stronie</h1>
<form onSubmit={this.handleSubmit} >
  <input onChange={this.handleChange} type="text" name="title" value={this.state.title} placeholder="title"/>
  <textarea onChange={this.handleChange} name="body" placeholder="body" value={this.state.body} cols="30" rows="10"></textarea>
  <button >Submit</button>
</form>

</>

     );
  }
}
 
export default App;


(package.json react-app)(proxy)

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "proxy": "http://localhost:8080"
}

(server node)

const express = require("express");
const morgan = require("morgan");
const axios = require("axios");
const concurrently = require("concurrently");
const mongoose = require("mongoose");
 
const app = express();
const PORT = process.env.PORT || 8080;
mongoose.connect('mongodb://localhost/baza_danych',{
    useNewUrlParser:true,
    useUnifiedTopology:true
} )

mongoose.connection.on("connected", (error)=>{
    if(error){
        console.log("cos poszło nie tak")
    } else{
        console.log("wszystko jest okay")
    }
})

app.use(express.json());
app.use(express.urlencoded({extended:false}));
app.use(morgan("tiny"));
const Schema = mongoose.Schema
const schemaBlogPost = new Schema({
    title:String,
    body:String,
    date:{
        type:String,
        default: Date.now()
    }
})
const BlogPost = mongoose.model('baza_danych', schemaBlogPost)

app.post('/api/save',(req,res)=>{
    const data = req.body
const newBlogPost = new BlogPost(data)
newBlogPost.save((error)=>{
    if(error){
        console.log("nie odebrano")
    }else{
        console.log("odebrano prawidłowo")
    }
})
})







app.listen(PORT, ()=>{
    console.log("server nasłuchuje");
})

96802599_2907513845963498_952929341765844992_n 97211102_240446973846131_6064808784788717568_n

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Ghost-strcommented, May 12, 2020

maybe I’m doing something wrong. I used axios in my frontend to send value from imput on url api/save. On server I used get with this url and save this in database. And it’s work but I then() don’t work in axios because is still pending.

you must answer on request and close connection on server. see http://expressjs.com/en/api.html#res

app.post('/api/save',(req,res)=>{
    const data = req.body
const newBlogPost = new BlogPost(data)
newBlogPost.save((error)=>{
    if(error){
        console.log("nie odebrano")
        res.status(400).send('error')
    }else{
        console.log("odebrano prawidłowo")
         // 200?
         res.status(201).send('saved')
    }
})
})
0reactions
karol-1commented, May 12, 2020

Ghost-str thank you a lot. It works perfectly well. I’m also grateful to everyone that tried to help and spent some time. 😃 I will read more express documentation and check await. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios post stay on pending - Stack Overflow
Network call was pending all the time and Mitigated it by passing the response back from server.js(route file) e.g(res.json(1);) and it ...
Read more >
Axios Post Request Stuck on Pending : r/nextjs - Reddit
I'm having issues with getting an axios request with an array of images to post successfully in nextjs. The axios request shows pending...
Read more >
[Solved]-Axios post stay on pending-Vue.js - appsloveworld.com
Network call was pending all the time and Mitigated it by passing the response back from server.js(route file) e.g(res.json(1);) and it resolved the...
Read more >
Elon Musk offers to proceed with Twitter deal - Axios
Driving the news: According to the letter, Musk has agreed to proceed in closing the transaction in accordance with the April 25 merger ......
Read more >
Cancel all axios requests in React's componentWillUnmount ...
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found