I have this array of object
const database = {
users: [
{
id:1,
name: "John",
email: "[email protected]",
passowrd: "h4ckM3!ifuc4n",
entries: 0,
joined: new Date(),
},
{
id:2,
name: "Mila",
email: "[email protected]",
passowrd: "mila",
entries: 0,
joined: new Date(),
},
{
id:3,
name: "Luke",
email: "[email protected]",
passowrd: "123",
entries: 0,
joined: new Date(),
}
]
}
I'm trying to query a specific element from that array
app.get("/profile/:id", function (req, res) {
console.log(req.params.id);
// const user = _.find(database.users, {id: req.params.id });
var user = _.find(database.users, {id:req.params.id});
console.log(user);
if(!user) {
res.status(404).json("no user found");
}
});
I kept getting
Example app listening on port 3002!
1
undefined
Any hints for me?
req.params.idwill be string...convert to numberconst user = database.users.find(({ id }) => id == req.params.id). The==comparison will compare numbers with numeric strings without requiring parsing