1

I need to check if the current user is the author of the question to allow him to see/use the delete question button. But with my implementation I can´t see the button at all:

<form @submit.prevent="deleteQuestion(question.id)">
    <input type="submit" v-if="this.currentUser === question.author" value="Löschen"/>
</form>

I get question.author with a JSON request , currentUser is set during the login.

Thanks for taking the time,

Fierl.

Decade Moon
  • 32,968
  • 8
  • 81
  • 101
Fierl
  • 11
  • 3

1 Answers1

0

this.currentUser and question.author are not the same objects, even though they might contain the same data. This is why the comparison fails.

Your user objects probably have an id property (or some other primary key). Compare against that instead.

<input type="submit" v-if="this.currentUser.id === question.author.id" value="Löschen"/>
Decade Moon
  • 32,968
  • 8
  • 81
  • 101
  • thanks, but if i don´t have an id for both of them? can I cast them somehow? – Fierl Jul 13 '17 at 11:59
  • If you not have any kind of unique key to compare them against (ID, username, email, etc), then there's no way you can compare them. You might be able to do a [deep comparison](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript) of the objects instead, if that works for you. – Decade Moon Jul 13 '17 at 12:01