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

167
Views
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);
over 4 years ago · Santiago Trujillo
2 answers
Answer question

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.

over 4 years ago · Santiago Trujillo Report

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")
   }
}
over 4 years ago · Santiago Trujillo 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!