Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda