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

238
Vistas
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 Respuestas
Responde la pregunta

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 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