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

119
Vistas
MongoDB query doesn't find the requested entries

i'm trying to show a user specific data using req.session.user and pass the ID to the criteria i'm building. (every entry also has a user field so i can match) yet it does not work.

The Service :

async function query(filterBy) {
  try {
    const criteria = _buildCriteria(filterBy);
    const collection = await dbService.getCollection('tab');
    const tabs = await collection.find(criteria).toArray();
    // const userTabs = await collection.find({ user: '62be030cb4de461a8462b863' }).toArray();
    return tabs;
  } catch (err) {
    logger.error('Can not find tabs', err);
    throw err;
  }
}

The console.log('userId', userId) returns the Id I get from my controller

function _buildCriteria(filterBy) {
  const criteria = {};
  const { text, genre, userId } = filterBy;
  console.log('userId', userId);

  if (text) {
    const txtCriteria = { $regex: text, $options: 'i' };
    criteria.name = txtCriteria;
  }
  if (genre) {
    criteria.genre = { $eq: genre };
  }
  if (userId) {
    criteria.user = { $eq: userId };
  }
  return criteria;
}

The controller :

async function getTabs(req, res) {
  try {
    const userId = req?.session?.user?._id;
    const filterBy = req.query;
    const fitlerUpdated = { ...filterBy, id: userId };
    const tabs = await tabService.query(fitlerUpdated);
    res.json(tabs);
  } catch (err) {
    logger.error('Failed to get tabs', err);
    res.status(500).send({ err: 'Failer ti get tabs' });
  }
}

I tried using

const userTabs = await collection.find({ user: '62be030cb4de461a8462b863' }).toArray()

and it works yet it doens't work along with the criteria.

thanks for any help!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I have realize I accidentally passed the wrong key. should have been id and not userId

about 4 years ago · Juan Pablo Isaza Denunciar
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