Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda