Home > OS >  Unable to get data from React App to Node server
Unable to get data from React App to Node server

Time:12-02

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

POST http://localhost:4200/search/ 404 (Not Found)

const [name, setname] = useState("")
  async function postName(e) {
        e.preventDefault()
        try {
            await axios.post("/search/", {
                name
            })
        } catch (error) {
            console.error(error)
        }
    }

app.get(`/search/`, (req, res) => {
    
    var {name} =req.body
    var Desc = {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);
      }
      return res.json(search);
    });
  });

CodePudding user response:

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)

CodePudding user response:

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);
  • Related