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

243
Visualizações
Mongoose - find() not returning anything when no parameters are passed, but returns data when parameters are passed

I have the following model in teamMembers.js:

const { Schema, model } = require('mongoose');

const teamMembersSchema = new Schema({
    uid: String,
    name: String,
    hours: Number
})

const TeamMembers = model('teamMembers', TeamMembersSchema);

module.exports = TeamMembers;

I've created the following endpoints in teamMemberRoute.js:

const TeamMembers = require('./models/teamMembers');

module.exports = (app) => {

    app.get('/api/pods/teamMembers/:uid', async (req, res) => {
        let teamMember = await TeamMembers.find( {'uid': req.params.uid } );
        return res.status(200).send(teamMember);
    });

    app.get('/api/pods/teamMembers', async (req, res) => {
        let teamMembers = await TeamMembers.find();
        return res.status(200).send(teamMembers);       
    });
}

The first endpoint (/api/pods/teamMembers/:uid) works just fine - when I pass a uid it returns documents specific to that uid in the TeamMember collection.

The second endpoint should return all documents from the TeamMember collection since no parameters are passed. However, when the request is executed, only [] is returned. We know for a fact that documents exist in the TeamMember collection, since the first endpoint returns data from that collection based on the uid parameter that is passed.

I'm stumped on this. Any ideas? I don't think there is anything wrong with my model since I am able to execute the first endpoint with no issues.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Express executes code from top to button, and that is the reason for this issue. It will match your first endpoint and assume that uid is null. Just change the order of defined endpoints, like this:

module.exports = (app) => {

    app.get('/api/pods/teamMembers', async (req, res) => {
        let teamMembers = await TeamMembers.find();
        return res.status(200).send(teamMembers);       
    });

    app.get('/api/pods/teamMembers/:uid', async (req, res) => {
        let teamMember = await TeamMembers.find( {'uid': req.params.uid } );
        return res.status(200).send(teamMember);
    });

}
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