Here is my global array with json in my users.js:
global.users = [
{
'id':1,
'name' : 'Dain',
'age' : 24,
'hobby' : 'gaming'
}
];
The router.get function:
router.get('/:userid', function(req, res){
for(let i = 0 ; i < global.users.length ; i++){
if(global.users[i].id === parseInt(req.params.userid, 10)){
return res.json({
user: global.users[i],
message : 'Success',
error: false
});
}
}
return res.status(404).json({
message : 'User Not Found',
error: true
});
});
However, the following function for getting two params does not work as expected:
router.get("'/:userid'+'+:age'", function(req, res){
for(let i = 0 ; i < global.users.length ; i++){
if( (global.users[i].id === parseInt(req.params.userid, 10)) && (global.users[i].age === parseInt(req.params.age, 10))){
return res.json({
user: global.users[i],
message : 'Success',
error: false
});
}
}
return res.status(404).json({
message : 'User Not Found',
error: true
});
});
Even if I pass age as follows in the url, it still returns the user info.
http://localhost:8080/users/1+226
Since 226 is not any of the ages defined, it must return:
{
message : 'User Not Found',
error: true
}
Even if I put any age, it still returns the values. Please guide.
http://localhost:8080/users/1+24