Mika Mika - 17 days ago 5
Node.js Question

Node array filtering not working

I am trying to filter an array as I have a million times in the past but it's not working.

locations = city.locations.filter(
(l) => {
console.log(l._id);
console.log(req.query.locationId);
console.log(l._id === req.query.location);
return l._id === req.query.location;
}
)


Console output:

l1dr2jmg42lb2sgiudi
l1dr2jmg42lb2sgiudi
false


The first two
console.log
print identical strings but the third prints false??!?

I checked that
city.locations
is an
Array
and both
l._id
and
req.query.locationId
are a
string
.

Answer

console.log(l._id === req.query.location);

you are not referring to req.query.locationId but toreq.query.location. Missing Id.

Comments