In the following function, I am comparing item.id to ID, "type of" shows that they are both strings but the comparison is failing. 1234 === 1234 returns false, for instance.
exports.handler = async (event) => {
if (!event.pathParameters || !event.pathParameters.ID) {
return Responses._400({ message: "missing ID" });
}
console.log("data ", data);
let ID = event.pathParameters.ID;
data.find((item) => {
console.log("item.id, ID", item.id, ID);
if (item.id.trim() === ID.trim()) {
return Responses._200(item);
}
});
return Responses._400({ message: "id not found", ID });
}
This is the array of data:
const data = [
{
id: "1234",
email: "user1@user1.com",
username: "user1",
createdAt: "2020-09-10T14:03:09.203Z",
__v: 0,
},
{
id: "5678",
email: "user1@user1.com",
username: "user1",
createdAt: "2020-09-10T14:03:09.203Z",
__v: 0,
},
];
The call to the function originates from the browser as follows:
https://hs40xxxxxx.execute-api.us-east-1.amazonaws.com/dev/get-user/1234