NicLovin
NicLovin

Reputation: 355

Pass query as parameter to express

I currently have an ejs file with a table element where each row is clickable like so: <tr onclick="myFunction(this)">

I use javascript to try and pass the query as a parameter in an http request:

<script>
        function myFunction(x) {
            num = parseInt(x.cells[0].textContent);
            window.location = '/shownumber?n=num';
        }
</script>

In my routes file I create the route as follows:

app.get('/shownumber', db.getSelectedNum)

I then have a queries.js file where I try to print out the value using console.log (for testing purposes):

const getSelectedNum = (request, response) => {
    console.log(n);
}

When I click on a table row though I get the following error:

ReferenceError: n is not defined

What am I doing wrong / how can I properly pass this parameter as a query so I can use it in the getSelectedNum function?

Upvotes: 0

Views: 380

Answers (2)

Sohan
Sohan

Reputation: 6809

If you are using express, it's already done for you. You can simply use req.query for that:

let n = request.query.n; // GET["n"]

Upvotes: 1

user16435030
user16435030

Reputation:

console.log(n) will break because you've never defined "n", it doesn't exist. In order to get it you need to use the request with something like request.query.

Upvotes: 2

Related Questions