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

129
Vistas
How does a function writer call the getByName . function

books-controllers

I want the data to appear by the name in the postman and not the ID because I have information and I want to fetch it through the name in the database

const getByName = async (req, res, next) => {
    const name = req.params.name;
    let book;
    try {
        book = await Book.getByName("name");
    } catch (err) {
        console.log(err);
    }

    if (!book)

        return res.status(404).json({ message: "No book found" });
    }
    return res.status(200).json({ book });
};

modelSchema

Here is the Skyma model

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const bookSchema = new Schema({
    name: {
        type: String,
        require: true
    },
    description: {
        type: String,
        require: true
    },
    price: {
        type: Number,
        require: true
    },
    avilable: {
        type: Boolean,
    },
    image: {
        type: String,
        require: true
    },

});

module.exports = mongoose.model("Book", bookSchema);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There in no in-built method in mongoose getByName. You can use generic find to search for object using name

let book = await Book.find({ name: name }).exec();

You can also use findOne if needed.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try this -

async function getByName(req, res){ 
   const bookname = req.params.name ;
   try {
      const book = await Book.findOne({name: bookname})
      if(book!==null) {
        res.status(200).send({'data': book}) ;
      }
      else {
        res.status(404).send("No book found !")
      }
   }
   catch(error) {
     console.log(error)
     res.send("Error")
   }
}
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