chibiw3n
chibiw3n

Reputation: 357

How to use axios to send request to a route that requires parameter?

I am trying to use axios to make a request to a route:

app.get("/mentorRequestAccept/:userId", userController.mentorRequestApprove);

which takes in a userId parameter. However, this is not working and I believe that I am not using using axios specifically the line: axios.get("/api/mentorRequestAccept?userId=", id);. Can someone verify that this is the correct syntax as I am not very familiar with it.

      approveUser(id){
        console.log("APPROVE BUTTON CLICKED", id);
        axios.get("/api/mentorRequestAccept?userId=", id);
      }

      render() {
        console.log("USERS", this.state.users);``
        return (
          <div className="mentorRequest">
            {this.state.users.map((user) => 
            <div>
                {user.username}
                <button onClick={() => this.approveUser(user._id)}>Approve</button>
                <button onClick={() => this.rejectUser(user._id)}>Reject</button>
            </div>)}
          </div>
        );
      }

Upvotes: 0

Views: 1091

Answers (3)

Pavel Alekseev
Pavel Alekseev

Reputation: 1222

You can pass params to config as a second parameter of axis.get function:

axios.get('/api/mentorRequestAccept/', {
  params: { id },
};

Upvotes: 0

Jackkobec
Jackkobec

Reputation: 6745

The simplest solution is string format:

axios.get(`/api/mentorRequestAccept/${id}`)

Or simple string concatination:

axios.get('/api/mentorRequestAccept/' + id)

Upvotes: 1

nalanart
nalanart

Reputation: 41

Try this:

approveUser(id){
        console.log("APPROVE BUTTON CLICKED", id);
        axios.get(`/api/mentorRequestAccept/${id}`);
}

Upvotes: 0

Related Questions