Search code examples
javascriptsqlaxiosknex.js

Trying to get a specific user using Axios get request not working. Should be a minor fix?


I figured it would work similarly to my delete request since it targets a specific user(delete request works perfect), so I passed an id as well to my get request. I logged the response of the request and I am given the error from my catch block in the file where I defined my sql queries using Knex, so i believe something is wrong with my sql query?.

In the logged response I can also see the id of the user im wanting to display which is a good sign, so I think it has to be a minor error somewhere either in my query or axios request to display a user.

I have provided a photo of the response I logged when i click the display user button: logged response from get request

// Display specific User
exports.userDisplay = async (req, res) => {
    // Find apecific user in database and display
    knex('users')
    .where('id', req.body.id) // find correct record based on id
    .then(() => {
        // Send users extracted from database in response
        res.json( { message: `This is the data we found: ${req.body.id} ` })
      })
      .catch(err => {
        // Send a error message in response
        res.json({ message: `There was an error retrieving user: ${err}` })
      })
}

    // Display User
    const handleUserDisplay = async (id: number, name: string) => {
        // Send GET request to 'users/all' endpoint
        axios
            .get("http://localhost:8000/users/display", { data: {
                id: id
              } })
            .then((response) => {
                // Update the users state
                setDisplayedUsers(response.data);
                console.log(response)
                // Update loading state
                setLoading(false);
            })
            .catch((error) =>
                console.error(`There was an error retrieving the user list: ${error}`)
            );
    };

Working delete query


// Remove specific user
exports.usersDelete = async (req, res) => {
 // Find specific user in the database and remove it
 knex('users')
   .where('id', req.body.id) // find correct record based on id
   .del() // delete the record
   .then(() => {
     // Send a success message in response
     res.json({ message: `User ${req.body.id} deleted.` })
   })
   .catch(err => {
     // Send a error message in response
     res.json({ message: `There was an error deleting ${req.body.id} User: ${err}` })
   })
}


Solution

  • GET requests don't have bodies. At least, in theory any HTTP request can, but you're looking for your value in the wrong place. Typically one would send the ID in the URL, so for a route matching:

    /users/display/:id
    

    you'd send:

    /users/display/12345
    

    using, I imagine, something like:

    axios.get(`http://localhost:8000/users/display/${id}`)
    

    On the server side, you'd receive your ID as req.params.id. OF course, I've made the assumption you're using Express, but you do seem to be!