Encountering an unusual reactivity problem involving Firebase (Firestore) when using Vue.js and Vuefire

I'm facing a strange issue and I'm completely stuck.

Here is the component in question:

<template>
  <v-card elevation="0">
    <h2>Accounts</h2>
    <v-simple-table fixed-header height="300px">
      <template v-slot:default>
        <thead>
          <tr>
            <th class="text-left">Account ID</th>
            <th class="text-left">Broker</th>
            <th class="text-left">Balance</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="account in accounts" :key="account.accountId">
            <td>{{ account.accountId }}</td>
            <td>{{ account.broker }}</td>
            <td>{{ accountBalances[account.accountId] }}</td>
          </tr>
        </tbody>
      </template>
    </v-simple-table>
  </v-card>
</template>

<script>
import { DB } from "../../firebase/db";

export default {
  name: "Accounts",
  data() {
    return {
      cashTransactions: [],
      accounts: [],
      newAccount: []
    };
  },
  computed: {
    accountBalances() {
      try {
        let balances = {};
        this.accounts.forEach(account => {
          let accBal = 0;
          const transactions = this.cashTransactions.filter(acc => {
            return acc.accountId === account.accountId;
          });
          transactions.forEach(transaction => {
            accBal += Number(transaction.amount);
          });
          balances[account.accountId] = accBal;
        });
        return balances;
      } catch (err) {
        console.error(err);
        return err;
      }
    }
  },
  firestore: {
    cashTransactions: DB.collection("cashTransactions"),
    accounts: DB.collection("accounts")
  }
};
</script>

<style scoped></style>

And here are the Firestore collections I'm working with:

What's puzzling me is the behavior of the Firestore when defined inside my components. If I set it up like this:

  firestore: {
    cashTransactions: DB.collection("cashTransactions"),
    accounts: DB.collection("accounts")
  }

Only the 'accounts' property seems to be reactive, meaning that adding documents to the collection reflects in real-time updates. However, setting it up as follows:

  data() {
    return {
      accounts: [],
      cashTransactions: [],
      newAccount: []
    };
  },
  firestore: {
    accounts: DB.collection("accounts"),
    cashTransactions: DB.collection("accounts")
  }

Still leads to only the 'accounts' being reactive. Even switching the order doesn't change the reactivity:

  data() {
    return {
      accounts: [],
      cashTransactions: [],
      newAccount: []
    };
  },
  firestore: {
    accounts: DB.collection("cashTransactions"),
    cashTransactions: DB.collection("accounts")
  }

Despite my attempts, the 'cashTransactions' property remains non-reactive and only gets updated on page refresh. Any insights on what could be causing this?

Thank you!

Answer №1

If you encounter issues with firestore():{}, consider implementing the following workaround:

watch: {
    cashTransactions(): {
        deep: true,
        handler(newArray) {
            console.log( 'Change detected...' );
        } 
    },
}

Feel free to give this a try.

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

Leveraging a direct SQL query in conjunction with Sequelize ORM and the literal method

Seeking guidance on updating the field level_id in Sequelize ORM, linked to a foreign key in a table named level_tbl. select * from level_tbl; +----------+----------+ | level_id | Level | +----------+----------+ | 1 | Higher | | 2 | Or ...

An issue arises with ReactJS MaterialUI Stepper when there is an overflow

My struggle with the Material UI Stepper component persists as I attempt to make it suit my requirements, specifically to display multiple steps and handle overflow. However, it stubbornly continues to misbehave by showing unusual separators when there is ...

The ng-route feature seems to be malfunctioning and I am unable to identify the error, as no information is being displayed in the console

Here is the code in my boot.php file where I have set up the links <ul class="nav nav-pills nav-stacked"> <li role="presentation"><a href="#/valley">Valley</a></li> <li role="presentation"><a href="#/beach"> ...

What is the process for uploading files with just node.js?

My dilemma involves a webpage featuring a form with a file input field. Upon submitting the form, I wish for the server to write the file on the server side. Despite numerous attempts to upload various test images using this form, each results in a plain ...

Unable to cancel the RTK query request

It's quite a dilemma. I need to handle the request differently when there is no user present. I've attempted different approaches like this and that const { data: carts = [] as ICart[], isFetching } = api.useGetCartProductsQuery(user.id, { skip: ...

How to turn off and on all elements within a Div tag

Is there a way to disable all elements within a Div tag? I've managed to disable input types successfully, but I'm struggling with links. I attempted the solution provided here, but it didn't work. Here's the code snippet that worked f ...

Tips for getting a sticky table header and including a limited number of columns, each with checkboxes or input fields

Encountering issues while trying to implement functionality with a jQuery library. One specific problem is the inability to interact with checkboxes on sticky columns, as well as difficulties clicking and typing in text fields. I am utilizing the jQuery S ...

When making a variable call outside of a subscriber function, the returned value is 'undefined'

I find myself in a situation where I have to assign a value to a variable inside a subscriber function in Angular. The issue is that the variable returns 'undefined' when called outside of the Subscribe function. Here's what I'm encount ...

Effective ways to manage extensive forms in Angular 2

I have a complex form in my application. What is the most effective way to collect data from this form and transmit it to the API using Angular 5? ...

A guide on utilizing multer-sftp for downloading files

I've been working on this code, but after searching online I still haven't found a way to download a file from the remote server. I can successfully upload files to the server, but downloading them is posing a challenge. var storage = sftpStorag ...

Using Vue.js and Laravel's permission system

Currently, I am working on the integration of Laravel Permission API with Vue.js frontend. My chosen library for Laravel Permission is from https://github.com/spatie/laravel-permission. However, I am facing a challenge in understanding how to verify perm ...

Design my div layout to resemble a tree shape

Take a look at this URL. I have dynamically created divs in a nested structure for a sports tournament. I need help styling the divs to match the tournament structure. This is how I want my structure to look. The code is functioning properly, it's ju ...

Adjust the database table when a session expires in PHP

Within my PHP code, there is a session timeout feature that triggers after 60 minutes of inactivity. This code snippet resides in the file /mno.php, where both the login and logout functionalities are also implemented. /mno.php if (isset($_SESSION[' ...

Newbie: Troubleshooting Vue Errors - "Vue is not recognized"

I'm currently at the beginning stages of learning Vue and am practicing implementing it. However, I keep encountering the errors "vue was used before it was defined" and "Vue is not defined". Below are excerpts from my HTML and JS files for reference. ...

Vue.js $scopedSlots do not function with Vue object

In the process of developing a Vue component that will be released once completed, I am wrapping Clusterize.js (note that the vue-clusterize component is only compatible with v1.x). The goal is to efficiently render a large list of items using Vue, particu ...

Algolia Vue Instant Search - When toggling a class, it resets the attributes in the Algolia search component

I'm facing an issue with my Algolia refinement list where the query disappears every time I toggle a class around it. I've been unable to pinpoint what is causing the values to reset. Below is an example of my current setup: <template> ...

end the node.js automated testing process

I'm currently using Jasmine and Zombie JS to create automated tests. I have integrated Drone.io for Continuous Integration, and the tests are executing successfully. However, there seems to be an issue where after passing the tests, the process does n ...

Clicking to reveal a v-date-picker v-menu and automatically focusing on a v-text-field within it?

I implemented a date-picker component in my app following the instructions from Vuetify. To enhance usability for desktop users, I removed the readonly attribute to allow manual input. Now, desktop users can easily navigate through form fields using th ...

Implement a jQuery DataTable using JSON data retrieved from a Python script

I am attempting to create an HTML table from JSON data that I have generated after retrieving information from a server. The data appears to be correctly formatted, but I am encountering an error with DataTable that says 'CIK' is an unknown para ...

Select any menu option to return to the one-page layout and then scroll down to find the location

I'm wondering if there is a way to navigate back from an external page to a specific section on my one-page website with a fixed menu? On my one-pager website, I have a fixed menu that includes a "apply for a job" button. When clicked, it takes users ...