My getByTitle query doesnt seem to work, all my other routes and models work perfectly using params. Not sure what it is I've written wrong?
For context this is the addBook code.
//Route
//Create a book
router.post("/books", async (req, res) => {
console.log(req.body);
try {
let body = req.body;
const newBook = await addBook(body);
res.json(newBook);
} catch (error) {
console.error(error.message);
res.json({ success: false });
}
});
//Model
async function addBook(body) {
try {
const res = await query(
`INSERT INTO my_books(author_firstname, author_lastname, title, publishedDate) VALUES ($1, $2, $3, $4) RETURNING *
`,
[
body.author_firstname,
body.author_lastname,
body.title,
body.publishedDate,
]
);
return res.rows[0];
} catch (error) {
console.error(error.message);
}
}
I've inserted only the relevant code, the above works fine, however the below constantly returns everything. In Postman I do the following:
Get: http://localhost:5000/books?title=title-name-here
with the body, raw, json:
{ "title":"title name here" }
//Route
//Get book by title
router.get("/books", async (req, res) => {
try {
let title = req.query.title;
let res = await getByTitle(title);
return res.json({ success: true, payload: res });
} catch (error) {
console.error(error.message);
res.json({ success: false });
}
});
//Model
async function getByTitle(body) {
try {
console.log("this is the value of title", title);
const res = await query(
`
SELECT * FROM my_books WHERE title = $1`,
[body.title]
);
return res.rows;
} catch (error) {
console.error(error.message);
}
}