-1

I don't understand what I am doing wrong. I have a simple code:

console.log(req.params.id); 
let tour = tournaments.find((item) => {return item.id === req.params.id});
console.log(tournaments);
console.log(tour);
console.log(req.params.id); 

Where tournaments is an array of objects. Here is my output:

1592563085412
[
  Tournament {
    id: 1592563085412,
    discipline: 'chess',
    type: 'Bracket Tournament',
    description: 'des 1',
    date: '2020-06-19 12:38:05'
  }
]
undefined
1592563085412

Why I am getting undefined after console.log(tour); What should I change? I expected to receive an object where id = 1592563085412

Thanks and Best Regards!

Karollo
  • 45
  • 7

1 Answers1

1

Make sure that both item.id and req.params.id are of the same type. For that u can add the logging:

console.log(typeof item.id)
console.log(typeof req.params.id)

If they are not of the same type, use == or better convert both to the same type. E.g. parseInt(req.params.id, 10). I expect req.params.id to be the culprit since request params are serialized to string.

See this Post: Which equals operator (== vs ===) should be used in JavaScript comparisons?

The strict equality operator (===) behaves identically to the abstract equality operator (==) except no type conversion is done, and the types must be the same to be considered equal.

kai
  • 6,702
  • 22
  • 38