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
CodePudding user response:
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
CodePudding user response:
unfortunately it's not helping. The result is still the entire table and not only the specific row