'get request with multiple parameters mySQL using node js
I'm writing REST API in node js. In my database I have two FK (user_A, user_B)
user_A - int (FK)
user_B - int (FK)
create_date - date
My GET request:
http://localhost:3000/chats?useridA=1&useridB=1
And my code:
app.get('/chats/:useridA/:useridB', (req, res) => {
mysqlConnection.query("SELECT * FROM Chats WHERE user_A_app_id = ? AND user_B_app_id = ?",
[req.params.useridA, req.params.useridB], (err, rows) => {
try
{
res.send(rows);
}
catch (err)
{
console.log(err.message);
}
})
});
The output in postman is the entire table and not the specific row. What's the problem in my code or in my get request?
- In general the "WHERE" statement in my code is not working with more then one parameters.
This is the "create table Chats" in my sql file for my database
Solution 1:[1]
your GET request is using query parameters:
http://localhost:3000/chats?useridA=1&useridB=1
So to use it inside your nodejs code, you can access those parameters us:
req.query.useridA
req.query.useridB
So the request should be like this:
app.get('/chats/:useridA/:useridB', (req, res) => {
mysqlConnection.query('SELECT * FROM Chats WHERE user_A_app_id =' +req.param("useridA")+ 'AND user_B_app_id =' +req.param("useridB")', (err, rows) => {
try {
res.send(rows);
}
catch (err) {
console.log(err.message);
}
})
});
or you just call get request using url parameters
http://localhost:3000/chats/:useridA/:useridB
http://localhost:3000/chats/1/1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ola Galal |
