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

109
Vistas
Express JS get function without asking user for an id

I'm having a problem with my get function for my express api. When testing it on postman, the request only works when the user inputs an oid in. However what im trying to do should just show the data from the whole table.

Here's my code, ive been trying to get the get to work without the oid's by using some tutorials but they've not been very good at explaining the whole thing.

async function getComments(req) {
   let status = 500, data = null;
   try {
      const oid = req.query.oid;
      if (oid
         && oid.length > 0 && oid.length <= 32
         && oid.match(/^[a-z0-9]+$/i)) {

         const sql = 'SELECT * FROM products';
         const rows = await db.query(sql, [oid]);

         if (rows) {
            status = 200;
            data = {
               'oid': oid,
               'productName': rows,
               'productCondition': rows,  
               'productDescription': rows,
               'productLocation': rows,

            };
         } else {
            status = 204;
         }
      } else {
         status = 400;
      }
   } catch (e) {
      console.error(e);
   }
   return { status, data };
}


app.get('/demo', async (req, res) => {
   const { status, data } = await getComments(req);
   res.status(status);
   if (data) res.json(data);
   else res.end();
})
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