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

272
Views
¿Cómo verificar si el usuario ya existe antes del registro?

Hola, quiero verificar si el usuario existe antes del registro, pero por alguna razón no funciona. algo malo está sucediendo después de otra cosa. ¿Puede alguien decirme por qué? gracias.

 //jj. using express const app = express() import express, { json } from 'express' //jj. main array const allUsers = [{name:"jane"},{name:"jacek"},{name:"morty"}]; //jj. main listener app.post('/adduser', (req, res) => { let tocomapre = req.body.name for (let user of allUsers ){ if(user.name == tocomapre){ console.log(user.name, req.body.name, tocomapre); console.log('user already exist'); res.status(400).send("User alredy exist") break } else{ try{ const user = {name:req.body.name} console.log(req.body.name); allUsers.push(user) return res.status(200).send('user on board') }catch{ res.status(500).send(" it's ain't good sthww") } } } }); ====================================================== //jj. request POST http://localhost:3000/adduser Content-Type: application/json { "name":"morty" }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tiene el else dentro del ciclo for para que un nuevo usuario se envíe a todos los allUsers si simplemente difiere del primer usuario existente. Esto está mal.

Reemplace el break con un return y coloque el bloque try-catch después del bucle:

 let tocomapre = req.body.name for (let user of allUsers){ if(user.name == tocomapre){ ... return } } try{ ... }catch{ ... }

Si el nuevo usuario se encuentra en algún momento durante el bucle for , emite un error 400 y return . De lo contrario, agrega el usuario.

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!