1

The Firestore database field (reviewPrivacy in the review collection) is of string type, populated from a Vue form entry (radio) with one of three three possible answers (values): keepFullyPrivate being among them.

I'm trying to only display <h2>The reviewer's identity is private</h2> if the value of review.reviewPrivacy is keepFullyPrivate.

Once this is working, I'll add a v-if-else and then v-else options, displaying different content for each.

My code is below.

There aren't any errors flagged in VSC, but it doesn't matter what the value of review.reviewPrivacy is - the text in the <h2> tags always displays, regardless of whether or not it's equal to keepFullyPrivate

<div v-if="('review.reviewPrivacy', '==', 'keepFullyPrivate')"><h2>The reviewer's identity is private</h2></div> 

Update (additional info):

  • I'm using Vue version 3.2.1
  • The data from Firestore is fetching correctly. For example, in the same parent as the code above, this line <p> Privacy choice for this review: {{ review.reviewPrivacy }} </p> results in the following text in the DOM: Privacy choice for this review: postAnonPublic, which is the v-else-if condition.

2nd update: code as a full block, as requested in comments:

<div class="review-detailZ">
<div> <!-- BEGIN main (left-hand) column -->
  <p> Privacy choice for this review: {{ review.reviewPrivacy }} </p>
  <br />
   
  <!-- Using Vue version 3.2.1 -->
          
   <div v-if="('review.reviewPrivacy', '==', 'keepFullyPrivate')"><h2>The reviewer's identity is private</h2></div>
   <div v-else-if="('review.reviewPrivacy', '==', 'postAnonPublic')"><h2>Incognito - review by{{ review.userName }}</h2></div> 
   <div v-else><h2>Reviewer chose to be fully public - full name is {{ review.userFirstName }} {{ review.userLastName }}</h2></div>
   
  <br />
  <p>Created {{ review.createdAt }} days ago</p>
  <br />
</div>

Thanks!

Mark Gavagan
  • 878
  • 12
  • 45
  • It's hard to figure out what's wrong because the code is split in different section. Can you share the complete complete (a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)) ? – Dharmaraj Aug 23 '21 at 13:15
  • 1
    Didn't `v-if="review.reviewPrivacy === keepFullyPrivate">` and `v-else-if="review.reviewPrivacy === 'postAnonPublic'"` work? Had you tried restarting Vue server after making this change – Dharmaraj Aug 23 '21 at 13:31

3 Answers3

1

('review.reviewPrivacy', '==', 'keepFullyPrivate') is just a comma separated group of strings, and it evaluates to the last string: 'keepFullyPrivate', so your markup becomes v-if="'keepFullyPrivate'", which is always truthy. Therefore, the div and its h2 are always rendered.

The correct expression to compare review.reviewPrivacy to 'keepFullyPrivate' is:

review.reviewPrivacy == 'keepFullyPrivate'

// or even better:
review.reviewPrivacy === 'keepFullyPrivate'

It's good practice to use triple-equals (===) for a strict comparison.

So the end result should be:

<div v-if="review.reviewPrivacy === 'keepFullyPrivate'"><h2>The reviewer's identity is private</h2></div>
tony19
  • 125,647
  • 18
  • 229
  • 307
  • Happy it worked for you. Restarting the dev server isn't normally needed. Out of curiosity, what dev server are you using? – tony19 Aug 23 '21 at 17:15
0

new Vue({
  el: "#app",
  data: {
    review: { reviewPrivacy:0 },
    keepFullyPrivate:0
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<div v-if="review.reviewPrivacy == keepFullyPrivate">
   <h2>The reviewer's identity is private</h2>
</div>
<div v-else>
<h2>else part</h2>
</div>
</div>
  • Thank you @k-sridhar-reddy. I tried your solution and it did not work for me at the time, BUT I had failed to take the important step of restarting the Vue server. once I did this, Tony19's solution worked (and your may also work, but I haven't texted it). Best, -M – Mark Gavagan Aug 23 '21 at 13:54
  • @MarkGavagan Thank you.., actually new Vue({ el: "#app", data: { review: { reviewPrivacy:0 }, keepFullyPrivate:'keepFullyPrivate' ---> //inplace of zero you need to place it is another way } }) happy to see you happy – K Sridhar Reddy Aug 23 '21 at 14:22
  • Gracias, @k-sridhar-reddy – Mark Gavagan Aug 23 '21 at 14:47
0

I see two ways to do this:

A) You can create a computed property "showSectionX ()" which does the comparison:

[...]

<div v-if="showSectionX"><h2>The reviewer's identity is private</h2></div>

[...]

computed: {
  showSectionX () {
    return this.review.reviewPrivacy === 'keepFullyPrivate'
  }
}

[...]

Instead if X you should of course use a word that describes the purpose of this section.

B) you can do the comparison directly in the v-if:

[...]

<div v-if="review.reviewPrivacy === 'keepFullyPrivate'"><h2>The reviewer's identity is private</h2></div>

[...]

I guess the latter is what you were trying to do but used the wrong syntax.

Fred
  • 1,103
  • 2
  • 14
  • 35
  • 1
    Thank you @Fred. Just before you posted your solution, I used advice by Dharmaraj to correctly implement Tony19's solution, which works perfectly. Best, -M – Mark Gavagan Aug 23 '21 at 13:52
  • 1
    If you are using this comparison in different places in your View or Component, I would advice for a computed property to avoid duplication. – Fred Aug 23 '21 at 13:55
  • 1
    Gracias @Fred. Will do! – Mark Gavagan Aug 23 '21 at 13:57