0

I want to check if an id is included in a list of items. The items are object and one of their fields is the id

If I log the ids for check purposes, this is the result:

60b23d56c60fdc3610a585f0 60b2443ac60fdc3610a585f5 false
60b24424c60fdc3610a585f4 60b2443ac60fdc3610a585f5 false
60b2443ac60fdc3610a585f5 60b2443ac60fdc3610a585f5 false // these items are equals but it still shows me false
60b2444bc60fdc3610a585f6 60b2443ac60fdc3610a585f5 false
60b24466c60fdc3610a585f7 60b2443ac60fdc3610a585f5 false

I use the mongoDB and NodeJs

This is my code:

let permissions = await PermissionModel.find({})
  .where("isDelete")
  .equals(false);
    
let rolePermissionInfo = await RolePermissionModel.findOne({ roleId: roleId, isDelete: false })
  .populate({
    path: 'roleId',
    model: 'Role',
    select: 'name'
  })
  .populate({
    path: 'permissionId',
    model: 'Permission',
    select: 'name id parentId'
  })
    
model.roleId = rolePermissionInfo.roleId.id;
model.roleName = rolePermissionInfo.roleId.name;
    
permissions.forEach((permission: any) => {
  rolePermissionInfo.permissionId.forEach((elment: any) => {
    model.claims.push({
      id: permission._id,
      isChild: false,
      parentId: permission.parentId,
      selected: element._id === permission._id ? true : false,
      name: permission.name,
    })
  });
});

What's the problem? Thanks in advance

axel
  • 3,778
  • 4
  • 45
  • 72
Mr-Programer
  • 541
  • 3
  • 8
  • 21

1 Answers1

2

They're most likely different types with identical values. Try comparing after casting them as the same type

selected: elment._id.toString() === permission._id.toString(),
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Kinglish
  • 23,358
  • 3
  • 22
  • 43