Trouble with axios ( pending all the time )
See original GitHub issueHello. 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");
})
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
you must answer on request and close connection on server. see http://expressjs.com/en/api.html#res
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!