I was trying to return a JSON Object but instead it returns an array. I am using primary key for query so I am sure I will only get one result.
This is my approach :
router.get("/student_info/:id", (req, res, next) => {
connection.getConnection((error, currentConnection) => {
if (!!error) {
console.log("Error occurred while connecting db")
} else {
let id = req.params.id;
currentConnection.query("SELECT * FROM students WHERE id=" + "'" + id + "'", (error, rows, fields) => {
if (!!error) {
console.log(error.message)
} else {
res.status(200).json(rows);
}
currentConnection.release();
});
}
});
});
What I want is this :
{
"id": "171-15-8966",
"name": "Alif Hasnain",
"course_code": "CSE412,CSE413"
}
But I get the result like this :
[
{
"id": "171-15-8966",
"name": "Alif Hasnain",
"course_code": "CSE412,CSE413"
}
]