manish
manish

Reputation: 1

Unable to get data from React App to Node server

So Am unable to make a search function i want to get a variable from search field and show the results that matched but am constantly getting this error variable undefined when i try to console.log it in the node server Edit-- i have already changed the axios.post to axios.get

app.get(`/search/`, (req, res) => {

let {name} =req.body
var Desc = name
console.log(name)
var Op= Desc+'%'
const q = "SELECT * FROM taric where Description LIKE ? ";
con.query(q,[Op], (err, search) => {
  if (err) {
    console.log(err);
    return res.json(err);
  }
  console.log(search);
  return res.json(search);
});

});

Upvotes: 0

Views: 38

Answers (2)

Sujit Libi
Sujit Libi

Reputation: 406

As you can see you are making POST request from frontend where as there is no POST request route to handle your request. As you have make route of GET for fetching the data from backend you need to make GET request from frontend as well. So you need to do as below:

axios.get(`your_endpoint_route_goes_here`);

instead of this:

axios.post(`your_endpoint_route_goes_here`, requestBodyObj);

Upvotes: 1

Aakash Uniyal
Aakash Uniyal

Reputation: 1559

HTTP methods are not the same. You are using app.get in the server while triggering a POST call from your client.

axios.post <-----> app.get (There is no route for POST call which client is expecting)

Upvotes: 0

Related Questions