Alwaysblue
Alwaysblue

Reputation: 11940

How can I pass entire react state with post request

I was thinking about passing entire react state with post request using axios

So, this is what I did

 axios.post("/profile", (
          {state: this.state}
        )).then(response => {
          console.log(response)
        }).catch(error => {
          console.log("this is error", error)
        })
        this.props.history.replace('/home');
      }

but Probably this is not how you send entire state with post request. So let's say this is my post request

state = {
  bestAchievement: "",
  secondaySports: "",
  dob: "",
  primarySports: "",
  role: "",
  role1: "",
  phone: "",
  userid: "",
  address: "",
  state: "",
  city: "",
  pinCode: "",
  bio: ""
}

Which I want send to my node route at once, How can I do it?

Ps: This is my node route

router.post("/", authCheck, (req, res) =>  {
    console.log("this is post data")
    console.log(req.body) //This is logging undefined

})

Upvotes: 1

Views: 1442

Answers (1)

Tholle
Tholle

Reputation: 112917

The second argument to axios.post is a key-value object with data you want to send to the server, so just this.state will work fine.

axios.post("/profile", this.state).then(response => {
  console.log(response);
}).catch(error => {
  console.log("this is error", error);
});

Upvotes: 2

Related Questions