Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
JavaScript - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status')

Ive been working on a login script for an app with node and express. Unfortunately i keep getting an the error "Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status')". Ive been trying to figure out what im doing wrong. Could someone please point out why this is happening?

Below is the code for the login script:

//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"})
})

Also the this how i am making the request to the 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 Respostas
Responde à pergunta

0

your problem is how you return the response (that you return it transformed to json() ) and how you capture the errors, I leave you a code example that can help you:

 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda