0

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);
});

});

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks i got it ,the error is resolved, but now am getting { name: undefined }
I assume you may have not pass the request body. But In case if you pass request body with GET method proxy issue may occur. If you want to pass request body you can go with different method like POST method. If you want to pass data with GET method you can pass data or id as query params. Hope this link will helps you: thecodebuzz.com/http-get-delete-request-body-guidelines . In this link you can see about Proxy Issue.
0

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)

1 Comment

yeah thanks the error is resolved . but i am still getting a empty set; if i print that variable it is showing { name: undefined }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.