0

I want to pass data from .ejs to my node API. For edit a record I'm passing id of the data.

For that I'm usign click event like this.

<input type="button" class="btn btn-primary" value="Edit" onclick="edit(<%= data[i].id %>)">

And I create a function in <script> tag.

function edit(id){
     alert(id);
     window.location.replace('/bill/edit?id='+id);
}

And my API is this.

router.get('/edit/:id', function (req, res, next) {
     console.log("Id is : " , req.query.id);
}

As per I used to before it's right I have search also on it. And it all shows this way but I don't understand that why it's not work for me.

When I run API in postman I got 404 Error.

<h1>Not Found</h1>
<h2>404</h2>
<pre>Error: Not Found
at E:\myapp\app.js:30:13
khushboo
  • 715
  • 2
  • 11
  • 24
  • Your router should be `/edit` instead of `/edit/:id` – vibhor1997a Mar 25 '18 at 13:59
  • yaaa thanks it works for me... Thank you . I was think that I want to pass id so I have to set `/edit/:id` but I was wrong ... Thanks again @vibhor1997a – khushboo Mar 25 '18 at 14:05
  • I was follow by this [Link](https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js) – khushboo Mar 25 '18 at 14:06

1 Answers1

0

Your API is '/edit/:id' but you are hitting '/bill/edit?'. May be that's why the you aren't able to pass the data to your API as they aren't same. Also, check the API endpoint once.