A guide on showcasing Firebase array elements in Vue.js

I have developed a Vue.js application that allows users to add items to an array stored in a Firebase document. The function responsible for adding items to the array in the database is functioning correctly. However, I am facing an issue where the entire array container is being displayed on the screen instead of rendering the individual items as list items. I am looking for a solution to style this in a way that the array items are neatly displayed as list items without the container. It would be great if I could achieve this using Vuetify. Thank you!

Template

<v-list class="elevation-1">
  <v-list-tile v-for="user in this.$store.getters.getUsers" :key="user.id" >
    <v-list-tile-content>
      {{ user.events }}
    </v-list-tile-content>
  </v-list-tile>
</v-list>

Method

    async addInput () {
      let finalInput = this.newInput
      let ref = await db.collection('users').where('user_id', '==', firebase.auth().currentUser.uid)
      .get()
      .then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
          console.log(doc.id, " => ", doc.data());
          doc.ref.update({'events': firebase.firestore.FieldValue.arrayUnion(finalInput)})
        })
      })
      .catch(function(error) {
        console.log("Error getting documents: ", error);
      });
    }

Answer №1

After some trial and error, I managed to achieve my desired outcome. I made some adjustments to the addInput method to store inputted data as objects in an array. Additionally, I implemented a second loop in the template to iterate through the items in the user.events array and display them as list items. The end result was successful! Take a look at the updated code snippet below:

Updated Template

<v-list class="elevation-1">
  <v-list-tile-content>
    <v-list-tile v-for="user in this.$store.getters.getUsers" :key="user.id">
      <v-list-tile v-for="newUser in user.events">
        {{ newUser.name }}
      </v-list-tile>
    </v-list-tile>
  </v-list-tile-content>
</v-list>

Updated Method

async addInput () {
  let finalInput = this.newInput
  let ref = await db.collection('users').where('user_id', '==', firebase.auth().currentUser.uid)
  .get()
  .then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
      console.log(doc.id, " => ", doc.data());
      doc.ref.update({'events': firebase.firestore.FieldValue.arrayUnion({
        'name': finalInput
      })
    })
  })
})
.catch(function(error) {
  console.log("Error getting documents: ", error);
});
}

Answer №2

When using the v-for attribute, it is important to place it inside the specific item elements rather than the container element. In order to do this correctly, simply move the v-for directive into the list item element as shown below:

<v-list class="elevation-1">
  <v-list-tile-content v-for="user in this.$store.getters.getUsers" :key="user.id">
    <v-list-tile v-for="event in user.events">
      {{ event }}
    </v-list-tile>
  </v-list-tile-content>
</v-list>

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

Utilize Chrome storage instead of localstorage to generate Parse sessions

I'm currently developing a Chrome Extension that relies on Parse User sessions. Because localstorage is limited to specific domains, I am looking to utilize chrome.storage so the data can be accessed across any site. The existing Parse Javascript SDK ...

Issue: Incorrect parameters for executing the MySQL statement

Currently, I am working on a nodeJs project and utilizing the npm package mysql2 for connecting to a MySQL database. This is how my MySql Configuration looks like:- let mysql = MYSQL.createConnection({ host: `${config.mysql.host}`, user: `${config.mys ...

Webpack Is Having Trouble Parsing My JavaScript Code

I recently started using webpack and I'm struggling to get it to work properly. I haven't been able to find anyone else experiencing the same issue as me. Every time I attempt to run "npm run build" to execute webpack, I encounter this error: ER ...

Is there a way to send a variable to the alert function using $.ajax()?

I need assistance with confirming the deletion of a record. When the user clicks on the button, it should send the value 'Yes' to a $_POST request in PHP for deleting the record. However, instead of working as expected, it is showing me the JavaS ...

Appending a forward slash at the end of a URL seamlessly directs the user to a serendipitous webpage experience, while

I'm currently developing a project on this website: Interestingly, when you append a forward slash to the end of the URL, all the images mysteriously disappear. Consequently, I am unable to include Google AdWords tracking code at the end of any URLs: ...

When the user clicks on a specific element, ensure that it is the main focus and generate an overlay

One of my challenges is implementing a custom element that captures user input upon clicking, focusing on it and overlaying other elements. I want the overlay to disappear if the user clicks outside the div. I attempted to achieve this using the iron-over ...

The trouble with dynamicHelpers in Nodejs Express

Trying to make my app run smoothly, I've set up the following configurations: app.configure(function(){ app.set('views', __dirname + '/views'); app.enable('jsonp callback'); app.set('view engine', 'j ...

incorporating my unique typographic styles into the MUI framework

I'm currently working on customizing the typography for my TypeScript Next.js project. Unfortunately, I am facing difficulties in configuring my code properly, which is causing it to not work as expected. Can someone kindly provide assistance or guida ...

What is the best practice for making a gRPC call within a Typescript Vue.Js component?

Upon reviewing the grpc documentation, I discovered that proto files can be used to generate Node (Javascript), Typescript with the assistance of grpc_tools_node_protoc_ts, and grpc-web. Given that performance is not a critical factor in my particular situ ...

Is it possible to have separate ports for front-end and back-end in NODEJS?

As I develop my NodeJS website, incorporating both front-end and back-end components, it is recommended to organize the files in such a way that the front-end specifically requests data from the back-end via an API. I am curious whether it is considered a ...

Can React tooltips have properties that allow for changing text styles, such as making text bold or unbold?

Seeking assistance on how to change bold text to regular text in react-tooltip. I have already installed npm react-tooltip. Note: The default text appears in bold and I would like it to be normal. ...

Enable Cursor Display in Readonly Input Fields

Consider this scenario: Setting an input field to .readOnly = true replaces the text cursor with a pointer arrow cursor, preventing users from entering or modifying the field. Interestingly, clicking into a readonly input field that already contains text s ...

Having trouble with Jquery and closures or function references?

It seems like the issue is related to a function running in a different scope where the jQuery library is not accessible. This can be seen when the function is called as the last parameter on the second line below. var funcExpandHeight = container.animate ...

What is the method for extracting a variable from a URL using JavaScript?

For instance, consider this URL- website.com/page/?var=20 I am looking to extract the "var" variable from the URL using JavaScript. By the way, my actual URL is- https://bipit2.pranaypro21292.repl.co/d21292auth/?code=pimWoHEuV7fgKEVQMTntRnzXqVla3G&gu ...

What are the steps to modify the relative URL when using Express proxy?

To prevent the common CORS issue with client-side JavaScript code, I utilize a Node.js Express server. The server is configured with the following code: var app = express(); app.all('/Api/*', function (req, res) { proxy.web(req, res, { ...

Basic animation created using Three.js

I'm currently experimenting with a basic color animation in Three.js. Below is the code I am using: const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geomet ...

A guide on integrating a route parameter into a Vue component's address

I have various components spread across different folders. For example, I have: .components ..folder1 ...component1.vue ...component2.vue ..folder2 ...component1.vue ...component2.vue My goal is to make my app dynamically search for the components based ...

What is the correct way to set up a click event listener for a radio button?

Currently, I am in the process of dynamically generating HTML radio buttons. Each button is assigned an Id stored in a variable. My goal is to add a click event handler to these radio buttons using the assigned Id. However, I am encountering an issue where ...

Guide to launching a web browser within a Phonegap app with a button click event

We are developing a PhoneGap application using HTML and JavaScript. Our goal is to integrate PayPal's website so that users can make payments with a simple button click event within the app, and then seamlessly return back to the application afterward ...

Why is the defaultDate property not functioning properly in Material-UI's <DatePicker/> component with ReactJS?

I recently implemented the <DatePicker/> component from Material-UI ( http://www.material-ui.com/#/components/date-picker ) in my project. I encountered an issue while trying to set the defaultDate property to the current date on my computer, as it r ...