简体   繁体   中英

Why is my logical comparison of the same number returning false?

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

You are using find and return in the wrong way.

See what gets returned with your current approach:

 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, }, ]; const ID = "1234"; const test = () => { data.find((item) => { console.log("item.id, ID", item.id, ID); if (item.id.trim() === ID.trim()) { return 200; } }); } console.log(test());

It's undefined .

Instead, you should do this:

 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, }, ]; const ID = "1234"; const test = () => { const found = data.find((item) => { console.log("item.id, ID", item.id, ID); return item.id.trim() === ID.trim(); }); if (found) { return 200; } } console.log(test());

Now you're returning status 200 from the outer function, not inside of the find function.

You can read more about how find works here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Essentially, inside of find() you should return false if it's not a match and true if it's a match.

from what I see you are not using find to return a value that qualifies itself rather you impose your own return there, find method actually returns the first element in the array that passes the condition so use the condition with return and try it the other way.

      Responses._200(data.find(item => {
       console.log("item.id, ID", item.id, ID);
       return item.id.trim() === ID.trim() 
      }));

or store the value of data.find in a value and send a response in another way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM