Reputation: 133
I am following some tutorial but there is some problem in finding value in array . This is how my code looks
const express = require('express');
const app = express();
const courses = [
{id: '1', name: 'ITCS1'},
{id: '2', name: 'ITCS2'},
{id: '3', name: 'ITCS3'},
];
app.get('/api/courses/:id', (req, res) => {
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send('Course not found for given id');
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}`));
when i am loading this url http://localhost:5000/api/courses/1
its giving me course not found for given id
But the expected result is {id: '1', name: 'ITCS1'}
Because id 1 is in courses array. So why he is showing error because id is matching with array ?
Upvotes: 0
Views: 2128
Reputation: 3613
It's work with this code:
const express = require('express');
const app = express();
const courses = [
{id: '1', name: 'ITCS1'},
{id: '2', name: 'ITCS2'},
{id: '3', name: 'ITCS3'},
];
app.get('/api/courses/:id', (req, res) => {
const course = courses.find(c => c.id === req.params.id);
if (!course) res.status(404).send('Course not found for given id');
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}`));
The reason why you get an error, because you're using parseInt
.
And in your course
, your id is a string, not integer. So, you should not to use parseInt
.
If you want to using parseInt, please update your course array, an make your id is an integer, not a string.
An example:
const courses = [
{id: 1, name: 'ITCS1'},
{id: 2, name: 'ITCS2'},
{id: 3, name: 'ITCS3'},
];
Now, you can use parseInt(req.params.id) and it's will working fine.
I Hope it's can help you.
Upvotes: 1
Reputation: 942
It's because you're using strict equal and you courses
has it's id as a string.
You can either change your courses
object to:
const courses = [
{id: 1, name: 'ITCS1'},
{id: 2, name: 'ITCS2'},
{id: 3, name: 'ITCS3'},
];
or use double equal:
const course = courses.find(c => c.id == req.params.id);
or parse both:
const course = courses.find(c => parseInt(c.id) === parseInt(req.params.id))
But I suggest changing the data type of courses.id
and using the strict equal. But that's only in my opinion. You can learn more about strict and loose equality here
Upvotes: 0
Reputation: 41
That's because c.id
is a string
but you are converting the id
parameter to int
with parseInt(req.params.id)
. '1'===1
is false
. Use this:
app.get('/api/courses/:id', (req, res) => {
const course = courses.find(c => c.id === req.params.id);
if (!course) res.status(404).send('Course not found for given id');
});
===
operator returns true only if the operands have the same value and type. Using ==
operator would work in your case but it's not recommended as it may lead false positives.
Upvotes: 0
Reputation: 888
This is because you’re comparing a string and integer strictly. You could either use two equal signs or change the data types to match.
Upvotes: 0
Reputation: 811
You have type string
of id
field. And you are matching it with int
type field. Make both field types
either make both of them int
or stirng
.
courses.find(c => c.id === req.params.id.toString())
Or
courses.find(c => parseInt(c.id) === parseInt(req.params.id))
Upvotes: 0
Reputation: 10060
Apply parseInt for both sides
courses.find(c => parseInt(c.id) === parseInt(req.params.id))
or use ==
not ===
courses.find(c => c.id == req.params.id)
Upvotes: 0