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

204
Views
JavaScript: no capturado (en promesa) TypeError: no se pueden leer las propiedades de undefined (leyendo 'estado')

He estado trabajando en un script de inicio de sesión para una aplicación con node y express. Desafortunadamente, sigo recibiendo el error "Error de tipo no capturado (en promesa): no se pueden leer las propiedades de undefined (leyendo 'estado')". He estado tratando de averiguar qué estoy haciendo mal. ¿Podría alguien señalar por qué sucede esto?

A continuación se muestra el código para el script de inicio de sesión:

 //logic for login page app.post('/api/login', async(req, res)=>{ const {email, password} = req.body //get user with the passed email and password const user = await userModel.findOne({email}).lean() if(!user){ res.json({status:"error", error:"Invalid username / password"}) } //check if the passwords match if(await bcrypt.compare(password, user.password)){ const token = jwt.sign( { id:user._id, username:user.username },JWT_SECRET ) return res.json({status: "ok", data:token}) } return res.json({status:"error", error:"Invalid uswername/password"}) })

También así estoy haciendo la solicitud a la API.

 const result = await fetch('/api/login',{ method: 'POST', headers:{ 'Content-Type':'application/json' }, body:JSON.stringify({ email, password }) }).then(res=>{ res.json() }) if (result.status === 'ok' ){ alert("Success") console.log(result.data) }else{ alert(result.error) } }```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

tu problema es como devuelves la respuesta (que la devuelvas transformada a json() ) y como capturas los errores, te dejo un ejemplo de código que te puede ayudar:

 const result = await fetch('/api/login',{ method: 'POST', headers:{ 'Content-Type':'application/json' }, body:JSON.stringify({ email, password }) }).then(res=>{ res }.catch(error=>{ error }) if (!result.ok) { const message = `An error has occured: ${result.status}`; throw new Error(message); } const data = await result.json(); return data; }
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!