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

139
Views
¿Cómo verifico si el nombre ya está tomado en la base de datos? Reaccionar + Axios

Quiero hacer un validador que valide la singularidad de los elementos. Si quiero crear cosas nuevas, primero quiero verificar si ya las tengo en la base de datos.

Código:

 const [categList, setCategList] = useState([]); const getItems = () => { // Sending HTTP GET request Axios.get(url).then((response) => { const categories = []; response.data.forEach((resCateg) => { categories.push(resCateg.name + ", " + resCateg.description); console.log(resCateg); }); setCategList(categories); }); };

Y antes de hacerlo:

 function submit(e) { e.preventDefault(); //reset form validation errors resetFormValidationErrors(); Axios.post(url, { // Sending HTTP POST request name: data.name, description: data.description, }).then((res) => { resetForm(); }); }

Quiero verificar en "preventDefault" que mi nombre es único. ¿Alguna ayuda?

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

0

Solo necesita almacenar nombres en su estado y luego puede verificar desde esa matriz si su nombre ya existe o no.

Aquí está cómo hacerlo-

 const [categList, setCategList] = useState([]); const getItems = () => { // Sending HTTP GET request Axios.get(url).then((response) => { const categoryNames = response.data.map(res => res.name) setCategList(categoryNames); }); };

Comprobar al enviar -

 function submit(e) { e.preventDefault(); //reset form validation errors resetFormValidationErrors(); // Checking here if `categList` already includes name if(categList.includes(data.name.trim())) { alert(`${data.name} is already taken, Please select any other name.`) return } Axios.post(url, { // Sending HTTP POST request name: data.name, description: data.description, }).then((res) => { resetForm(); }); }
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!