Home > Software design >  How to use the message that i sent from the backend NodeJS to on React js
How to use the message that i sent from the backend NodeJS to on React js

Time:08-20

I have this login method on the express server and i wanna display the "invalid email or password!" for example on the react component how do i retrieve the message from backend to frontend. this is the express login route:

app.post('/login', async(req, res)=>{
const id = req.body.id
const password = req.body.password
try {
    const user = await User.findOne({id: id, password: password})
    if(!user){
        res.send({message: "invalid email or password!"})
    }
    if (user){
        const token = jwt.sign({
            jwtid: id,
            jwtpassword: password,
        }, process.env.JWTPrivateKey)
        res.send({message: "logged in succesfully", token: token})
    }
 } catch (error) {
    console.error(err.message)
}
})

And this the Axios methode to send data to the backend:

const LoginUSer = (e) =>{
    e.preventDefault();
    Axios.post('http://localhost:3001/login',{
    id: id,
    password: password,
})
}

CodePudding user response:

Heloo

axios.post('login', {
id: id,
password: password,})
.then(function (response) {
console.log(response);})

Add .then()

  • Related