0

I'm trying to see inline if an array from a mongodb database contains a specific object but I just can't get it to work.

I have been searching all around for different solutions and tried all of them; Array.some, Array.includes, Array.indexOf etc etc.

I'm quiet new to this so I'm probably under some misunderstanding, does anyone have an idea what I'm doing wrong? The app starts as normal and no error messages but it's saying everyone is a "leader" while they are not.

<div class="ui three buttons">
        <% if(task.leaders.includes(currentUser)){ %>
          <button form="deleteTaskForm" class="ui basic negative button"><i class="ui icon trash alternate"></i></button> 
          <a class="ui basic orange button" href="/task/<%= task._id %>/edit"><i class="ui icon edit"></i></a> <% }
          else{ %>
          <div style="opacity: 40%;" class="ui basic negative button popupButton" data-content="Only the leader of the task can remove this task."><i class="ui icon trash alternate"></i></div>
          <div style="opacity: 40%;" class="ui basic orange button popupButton" data-content="Only the leader of the task can edit this task."><i class="ui icon edit"></i></div>
          <% } %>
        <button form="doneTaskForm" class="ui basic green button"><i class="ui icon check square"></i></button>
      </div>

Array.prototype.some()

<div class="ui three buttons">
        <% if(task.leaders.some(leader => leader._id == currentUser._id)){ %>
          <button form="deleteTaskForm" class="ui basic negative button"><i class="ui icon trash alternate"></i></button> 
          <a class="ui basic orange button" href="/task/<%= task._id %>/edit"><i class="ui icon edit"></i></a> <% }
          else{ %>
          <div style="opacity: 40%;" class="ui basic negative button popupButton" data-content="Only the leader of the task can remove this task."><i class="ui icon trash alternate"></i></div>
          <div style="opacity: 40%;" class="ui basic orange button popupButton" data-content="Only the leader of the task can edit this task."><i class="ui icon edit"></i></div>
          <% } %>
        <button form="doneTaskForm" class="ui basic green button"><i class="ui icon check square"></i></button>
      </div>

EDIT: Solved. This is the solution that got it to work for me.

<div class="ui three buttons">
        <% if(task.leaders.some(leader => leader._id.equals(currentUser._id))){ %>
          <button form="deleteTaskForm" class="ui basic negative button"><i class="ui icon trash alternate"></i></button> 
          <a class="ui basic orange button" href="/task/<%= task._id %>/edit"><i class="ui icon edit"></i></a> <% }
          else{ %>
          <div style="opacity: 40%;" class="ui basic negative button popupButton" data-content="Only the leader of the task can remove this task."><i class="ui icon trash alternate"></i></div>
          <div style="opacity: 40%;" class="ui basic orange button popupButton" data-content="Only the leader of the task can edit this task."><i class="ui icon edit"></i></div>
          <% } %>
        <button form="doneTaskForm" class="ui basic green button"><i class="ui icon check square"></i></button>
      </div>
user3313308
  • 25
  • 1
  • 6

1 Answers1

0

Comparing object is heavy task on javascript, the solution you need compare unique object property

if (task.leaders.some(lead => lead.user.id == currentUser.id)) {
  // Do something
}

Read Array.prototype.some() here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

You can also using https://lodash.com/docs/#isEqual if your case need to compare object

EDIT:

If you comparing mongodb ObjectId you need do this https://stackoverflow.com/a/11638106/10216251 raw result of ObjectId is not plain string so you can't compare using ==

Ardy Febriansyah
  • 650
  • 1
  • 6
  • 14