Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Postgresql req.query returning everything instead of what is being queried?

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);
  }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!