Conditionally display a button utilizing V-IF when a Firestore value evaluates to true

I am fetching data from my Firestore database and displaying them on cards. I want to add buttons to the card only if a specific value in the loaded object is true.

{ "Alarm": false, "Safety": true, "Battery": true }.

For example, if the values are as above, I would like to display a safety button and a battery button but not an alarm button.

I have tried using v-if and a created function, but I'm unable to make it work no matter what approach I take.

The documents are stored in an array called users after being fetched, and reading them poses no issues.

Despite referencing several online resources and attempting different methods such as v-if=(users.Alarm) or using a combination of a created function and v-if, nothing seems to be effective in implementing the desired functionality.

<v-card  class="mx-4 grey darken-4" v-for="user in users" :key="user.id">

     <v-footer flat height="35px" color="black"> 
       <v-btn class="green--text mx-1" x-small v-if="safety">Safety</v-btn> 

       <v-btn class="red--text mx-1" x-small v-if="alarm">Alarm</v-btn> 

       <v-btn class="yellow--text mx-1" x-small v-if="battery">Battery</v-btn> 

     </v-footer>
     <v-divider></v-divider>
</v-card>

export default {
  components: { },
  data() {

    return {
      safety: false,
      battery: false,
      alarm: false,
      users: [],
    };
  },

  created() { 
    if (users.Safety) {
       this.check = true;

   }},
// other buttons will go here once I get one working


   var user = firebase.auth().currentUser;
   var SuperUser = user.uid + "3329"

    db.collection(SuperUser)

      .get()
      .then(querySnapshot => {
        querySnapshot.forEach(doc => {
          const data = {

            id: doc.id,
            Safety: doc.data().Safety,
            Alarm: doc.data().Alarm,
            Battery: doc.data().Battery,

          };

          this.users.push(data);
        });
      })
     }
    }; 

I want to display the buttons on the card only when their respective values are true, which are boolean values stored in the database. If true, show the button; otherwise, hide it. Any assistance in achieving this feature would be greatly appreciated. Thank you.

Answer №1

The solution turned out to be straightforward, just a minor error on my part - I mistakenly used users.Alarm instead of user.Alarm in the v-for loop. Appreciate all your input that ultimately helped me pinpoint the issue. My apologies for any confusion caused!

Answer №2

It appears that the relevant data is not being properly set after receiving the API response. It is recommended to assign values for safety, battery, and alarm within the callback function. Here's an example of how this can be done:

querySnapshot.forEach(doc => {
      const data = {
        id: doc.id,
        Safety: doc.data().Safety,
        Alarm: doc.data().Alarm,
        Battery: doc.data().Battery,
      };
       this.users.push(data);
       this.safety = doc.data().Safety;
       this.battery = doc.data().Battery;
       this.alarm = doc.data().Alarm;

    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The object contains an incorrect property that shouldn't be there

I am currently working on an application using NestJS with the Fastify adapter But I am encountering something strange with object construction. Going through all the related classes and methods: Controller endpoint handler @Get() @ApiOperation ...

What is the process for retrieving, giving back, and removing a randomly selected element from an array?

Is there a way to efficiently retrieve, remove, and replace a random element from an array? The code for the array is shown below: String arr[3] = {"Cat", "Dog", "Mouse", "Horse"}; I appreciate any guidance on this matter. Edit Also, this functionality i ...

Keeping the header fixed on a sticky div (tocbot)

While working on my website, I decided to implement a Table of Contents section using tocbot. However, I encountered an issue where the Title I added above the table doesn't stay fixed when scrolling. https://i.stack.imgur.com/vCm1K.gif Here's ...

The failure callback was triggered even though the Ajax request was successfully executed and the server responded with a 200 status

On my HTML5 test webpage test.html, I have implemented a cache manifest. The page makes an Ajax request to the server for a webpage called do_get_data.php, which is included in the NETWORK: section of the cache manifest file. When testing, Firefox 10 succ ...

Is there a way to trigger a "click" on a webpage without physically clicking? So that I can preview the response message before actually clicking

Is it possible to generate a "mock" request to a server through a website in order to examine the response before sending the actual request? Let me clarify with an example: if I were to click on a button on a website, it displays a certain message. I am ...

Verify the data provided by the client on the client side and cross-reference it with the data stored in mySql database on

Assistance is needed in comparing and validating data from client-side to server-side to determine the validity of an email. The process involves the client entering an email, clicking a verification button, which triggers a check on the server side to see ...

Submit the form to MailChimp and then show a personalized success page

I am currently developing a MailChimp subscription form that is set to send the form $_POST data to MailChimp, without loading the success page. In other words, I aim to trigger custom JS upon submission. The code snippet below outlines my progress so fa ...

When using the findById method in mongoose, it may return a null value for a

I've created a schema named subcategory: const subCategorySchema = new mongoose.Schema({ name: { type: String, required: true }, description: { type: String, required: false }, category: { t ...

jQuery Mobile persists in loading the initial page of a multi-page document after form submission

After searching extensively for an alternative to data-ajax="false" without success, I've found myself in a dilemma. In my Phonegap application, utilizing jQuery Mobile to submit a form requires running it through the standard submit function in jQuer ...

Error message: "AJAX preflight request access control check failed"

Having an issue with sending a POST request to the SendGrid email API. Whenever I try to submit the form for the POST request, I encounter this error in the console: Failed to load https://api.sendgrid.com/v3/mail/send: Response to preflight request doe ...

How to delete an item from an array of objects using ReactJS

Hello there, I am currently learning about React Js and I have encountered a problem. I need to remove an element from an array. Here is the structure of my array: array = [{ Id: "L0", value="asas"}] The array that I want to work with is stored in this ...

Is there a way to retrieve the value from a select tag and pass it as a parameter to a JavaScript function?

I would like to pass parameters to a JavaScript function. The function will then display telephone numbers based on the provided parameters. <select> <option value="name-kate">Kate</option> <option value="name-john">John& ...

Is the shape of the Shadow in Three.js r76 MeshLambertMaterial unusual?

When using r70, the shadow appears as expected - r70 example (Shadow correct shape) However, with r76, the shadow appears abnormally shaped - r76 example (Shadow abnormally shaped) It is noticeable that the shadows on the MeshLambertMaterial on the groun ...

Firefox is mistakenly interpreting a pasted image from the clipboard as a string instead of a file, causing

I am facing an issue where I am attempting to extract images from a contenteditable div using the paste event. The code works perfectly in Chrome but does not function as expected in Firefox. I have implemented the following code: $(window).on("paste& ...

The Ajax request is not successfully communicating with the PHP script

I am struggling with an issue regarding ajax calling a PHP code without changing the current page. Despite checking the php_error.log, I cannot find any reference to the PHP file. There are no errors displayed on screen, leaving me clueless about how to re ...

Issue with positioning in Excanvas VML

After struggling through the implementation of excanvas on ie8, I managed to sort out the dynamic element issue. The once hidden elements are now rendering properly throughout most of the app, but just when it seemed like all was going well, another proble ...

Tips for optimizing large image files on a basic HTML, CSS, and JavaScript website to improve site speed and ensure optimal loading times

Currently, my site is live on Digital Ocean at this link: and you can find the GitHub code here: https://github.com/Omkarc284/SNsite1. While it functions well in development, issues arise when it's in production. My website contains heavy images, in ...

Having trouble parsing an XML received from AJAX request

I am encountering an issue with the code below: $.ajax({ type: "POST", dataType: "xml", url: getUrl('/GetPeriodicStats/'), data: XML.innerHTML,//some xml, success: function(c) { After receiving the XML data in the clie ...

Monitoring modifications to nested information in Vue.js

I am looking to monitor changes in the 'families' variable, which contains nested objects. <component-test v-for="family of familiesToDisplay" // rest /> data: () => ({ families: [], }), computed: { ...

What is the most efficient way to move selected products to the payment page when the checkout button is clicked in React?

Need: I need to move all the selected products from the shopping cart to the payment page once the user clicks on the checkout button. List of products in the shopping cart: https://i.sstatic.net/31KpH.png The selected products should appear on the Payme ...