beginner who does not know how to request/BEGGINER TRYING TO REQUEST

Asked

Viewed 37 times

-1

I am doing a project where I need to access a user ID (the database is just an obj for now). however when trying to make a destructin:

---> const {id}= req.body; <---

let found= false;

database.users.map(user=>{
    if(user.id===id){
        found = true;
        return res.json(database.users);
    }
})

my destructin ID has no value. i have a Login for the project, and when soon with the user I have an id. this user’s Obj is given as an answer after Login, and should be compared to the user.id ID for my project to work. but I am not able to use the information my Login expele.

my "login" is this, if useful:

app.post("/signin",(req,res)=>{
if(req.body.email=== database.users[0].email && 
   req.body.password===database.users[0].password){
    res.json(database.users[0]);
}else{
    res.status(400).json("erro de login");
}

})

from now on Agradeco ;)

1 answer

0

Have you tried using the find() method? He will return you the first element he finds that meets your request

const found = database.users.find( user => user.id === id );

If the found variable receives some value it does not return false, if it does not find it it will remain empty

**Edit: If that’s not the problem, try to illustrate it better

  • I liked the method, but this condition continues to give an indefinite value. the problem itself is in the proper id of (const {id}= req.body;), it is Undefined, but it should contain the id that comes from my login response. I enter the application, with email and password and login responds to the obj corresponding to this data, it is from this obj that I want the id of distructin, but I am not able to reference I think

  • req.body id comes from the request, from the json file you send via post...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.