Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
¿Cómo llama un escritor de funciones a getByName . función

libros-controladores

Quiero que los datos aparezcan por el nombre en el cartero y no por el DNI porque tengo información y quiero buscarla a través del nombre en la base de datos

 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 }); };

modeloEsquema

Aquí está el modelo Skyma

 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 answers
Answer question

0

No hay un método incorporado en mongoose getByName . Puede usar la búsqueda genérica para buscar objetos usando el name

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

También puede usar findOne si es necesario.

about 4 years ago · Juan Pablo Isaza Report

0

Puedes probar esto -

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!