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

227
Views
If request body is exist it should give me 409

*Anyone help please I try to avoid the request if the name exists with NodeJS express *

here is my code


/* Post new person to persons */
app.post("/api/persons/", (req, res) => {
  const schema = {
    name: Joi.string().alphanum().min(3).max(30).required(),
    number: Joi.string().min(9).max(30).required(),
  };
  const { error } = validateFunction(req.body);
  const { body } = req;
  const reqValue = persons.some(
    (person) => person.name.toLowerCase() === body.name.toLowerCase()
  );
  if (error) return res.status(400).send(error.details[0].message);
  else if (reqValue)
    return res.status(409).send(`${body.name} is already exist`);
  else {
    const person = {
      id: persons.length + 1,
      name: req.body.name,
      number: req.body.number,
    };

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I solved the issue:

/* Post new person to persons */
app.post("/api/persons/", (req, res) => {
    const schema = {
        name: Joi.string().exist().alphanum().min(3).max(30).required(),
        number: Joi.string().min(9).max(30).required(),
    };
    const { body } = req;
    const { error } = validateFunction(body, schema);
    const result = persons.filter(({ name }) => name === body.name);

    console.log("resulit", result);
    console.log("The name of request", body.name);

    if (error) return res.status(400).send(error.details[0].message);
    else if (result && result.length > 0) return res.status(409).send(`${body.name} is already exist`);
    else {
        const person = {
            id: persons.length + 1,
            name: req.body.name,
            number: req.body.number,
        };
        persons.concat(person);
        res.send(person);
    }
});
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!