Leverage all the documents obtained from a collection to reference in a Vue component

I am attempting to display all documents from a Firestore collection in a table using refs, but I am unsure about accessing each field for template ref.

getDocs(collection(db, "usr"))
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      console.log(doc.data())
    });
})

Is there a way to access the doc.data() from template ref similar to this?

const employees = ref([
{
    name: 'John Doe',
    title: 'IT Technician',
    department: 'IT Department',
    employment: 'Probation',
    status: 'Terminated',
    joined: '16 Jul 2021',
    email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9df7f2f5f3b3f9f2f8ddf8e5fcf0edf1f8b3fef2f0">[email protected]</a>',
},
{
    name: 'Jane Doe',
    title: 'Marketing',
    department: 'Marketing Department',
    employment: 'Permanent',
    status: 'Active',
    joined: '16 Jul 2021',
    email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0aaa1aea5eea4afa580a5b8a1adb0aca5eea3afad">[email protected]</a>',
},

       <tr v-for="employee in employees" :key="employee.email">
            <td >
              <div>
                  <div>
                    {{ employee.name }}
                  </div>
                  <div>
                    {{ employee.email }}
                  </div>
              </div>
            </td>
            <td>
              <div>{{ employee.title }}</div>
              <div>{{ employee.department }}</div>
            </td>
            <td>
              <span> {{employee.status}} </span>
            </td>
            <td>
              {{ employee.joined }}
            </td>
            <td>
              {{ employee.employment }}
            </td>
            <td>
              <a href="#">Edit</a>
            </td>
      </tr>

I am facing difficulty in accessing the doc.data() outside of the function or promise. I am utilizing script setup.

Answer №1

For a successful solution, try implementing the code snippet below:

fetchData(collection(db, "users"))
  .then((querySnapshot) => {
    const resultArray = [];
    querySnapshot.forEach((doc) => {
      resultArray.push(doc.data());
    });
    employees.value = resultArray;
})

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

Surprising "unexpected end of line" JavaScript lint notification out of nowhere

In this simplified version of my JavaScript code: function addContent() { var content = []; content.append( makeVal({ value : 1 }) ); // lint message generated } After running a lint program, I received the followi ...

Transfer your documents effortlessly as soon as they are

I am having trouble implementing an automatic file upload feature without the need for a separate upload button. Below is the code I have so far, excluding the irrelevant CSS. //html <!-- some codes here--> <input type="file" id="f ...

Button with toggle capabilities for dropdown menus (Vue and Laravel)

Currently I am in the process of learning vue and attempting to construct a reusable dropdown component. After dedicating hours to this task, I have successfully put it all together. However, I have hit a roadblock as I simply want to transfer the button n ...

Navigating with Angular - sending users to an external webpage?

When working with AngularJS routes, there is the option to use an otherwise route as a replacement for a 404 error: $routeProvider .when(...) .otherwise({ redirectTo: 'my/path' }); Is it possible to configure the otherwise route to redirect ...

Encountered an error while trying to access an undefined property during an Angular Karma

I'm currently working on testing a service function that involves multiple $http.get() calls. The function being tested returns a promise but the test is failing with an error stating response is undefined. Below is the test script: it('should ...

What is the best way to ensure buttons are always visible and functional in a Vuetify card, regardless of whether the contents are hidden

Click here to view the current code on CodePen How can I ensure that the buttons remain fixed at the bottom, even when the user selects "yes" in the radio button? <div id="app"> <v-app id="inspire"> <v-layout> <v-flex xs1 ...

Angular Universal experiences issues with route functionality after page reloads

Recently deployed an Angular Universal web app on Firebase. While all routes within the application are functioning properly, encountering errors when trying to access them externally. Below is the error message: functions: Starting execution of "ssr" ⚠ ...

Searching for data in a bootstrap table that is not case-sensitive

Searching for and highlighting text within a bootstrap table has proven to be a challenge. Check out the Fiddle for an example: https://jsfiddle.net/99x50s2s/74/ HTML <table class='table table-bordered'> <thead> <tr& ...

transferring function from server to the client module named Next 13

After referencing the documentation for the app directory in Next.js, it is recommended to fetch data inside Server Components whenever feasible. Server Components always carry out data fetching on the server. This advice is particularly helpful for me as ...

Achieving Compatibility Between jQuery 3.6.0 and Node.js v12.16.1

Utilizing an online IDE known as Replit, I am working on node.js projects running on the node version: 12.16.1. However, my current challenge lies in attempting to make jQuery 3.6.0 compatible with this particular node.js version. Despite trying various me ...

The subsequent block within the code is being initiated following the initial block in Node.js

It was expected that "1" would be printed before "2", but the output is incorrect. fs.readdir("./my_stocks", (err, files) => { for(each in files){ var file=files[each]; if(file!='portfolio.js'){ var fn="./my_sto ...

What is the importance of context in the subscription method of RxJS or Angular Observables?

In the given situations, I am providing a child Component with a property that is updated later through an RxJs Observable subscription. Angular fails to detect changes when not using an anonymous function or binding the this context. // Situation 1 // C ...

Turn on the text field when the enter key is pressed

I've searched online for solutions, but none of them have resolved my issue. Upon loading my page, I am able to successfully have my JS file select the first textfield. However, I am struggling with getting it to proceed to the next textfield when th ...

Running the `npm run build` command excludes the `.cache` folder from the `

Currently, while working on a Vue Application, I have encountered an issue. When I run the command npm run build to generate the dist folder, a new folder named '.cache' is automatically created within node_modules. This folder contains files wit ...

Reacting to a click event to display a new image

I'm looking to create a webpage with multiple images where clicking on one image will open another image with its description. The images are stored locally in my library. I tried using the href attribute in the html tag, but it doesn't accept lo ...

Is there a way to effectively transfer both Vertex and Face normals to a Three.js shader?

It seems that the THREE.Geometry methods, .computeFaceNormals() & .computeVertexNormals(), assign values to a built-in attribute array called "normal." If I want to utilize both vertex- & face-normals in one shader, I need to: Calculate face-normals ...

Tips for building a task list using JavaScript only

Is it possible to create a to-do list using only JavaScript in an HTML file with a single div tag? Here is my HTML file for reference: example Here is the structure of my HTML file... <!DOCTYPE html> <html lang="en"> <head> ...

What could be causing the QullJS delta to display in a nonsensical sequence?

The outcome showcased in the delta appears as: {"ops":[{"retain":710},{"insert":" yesterday, and she says—”\n“The clinic?","attributes":{"prediction":"prediction"}},{"del ...

Determine the vertical dimension of an element through a JavaScript event listener

I've been working on creating an Apple-style image sequence scroller from a codepen demo. Here's the link to the original: https://codepen.io/jasprit-singh/pen/LYxzQjB My goal is to modify the JavaScript so that the scroll height is based on a p ...

angular-ui-tree, dynamically generate the tree using data retrieved from the DATABASE

I have implemented the angular-ui-tree library to present the folder hierarchy. The nodes are saved in a MongoDB database, with each node object structured as follows: { "node_name" : "Folder 1", "node_path" : "AAABBB", "node_id" : 103, "node ...