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:

https://i.sstatic.net/hhJdz.png

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

The issue persists with react-hook-form and Material UI RadioGroup as the selected value remains null

Having trouble with RadioGroup from Material UI when using react-hook-form Controller. I keep getting a null selected value, even though my code seems right. Can you help me figure out what's missing? import * as React from "react"; import { ...

Using BeautifulSoup to Retrieve JPEG from an Image Tag's Src Attribute

Struggling with scraping this webpage for personal use. I am having trouble extracting the thumbnails of each item on the page. Despite being able to see image tags containing the required .jpgs when using "inspect" to view the html DOM, I cannot find the ...

Include a selection of images within a popover box

I am currently working on a component that contains various experiences, each with its own popover. My goal is to display an array of images within each popover. Specifically, I have different arrays of images named gastronomiaExperience, deportesExperien ...

Encountering a problem with parsing a JSON object using the .map

After receiving this JSON response, my main goal is to extract the value located in the identifier. By utilizing console.log, I am able to analyze the structure of the object: Object {rows: Array[33], time: 0.015, fields: Object, total_rows: 33} fields: O ...

ReactJS: Trouble encountered while parsing information from JSON data

Currently, I am facing an issue while trying to pass data from my JSON file to my ReactJS application. The error message that I am encountering is as follows: TypeError: Cannot read property 'mainPage' of undefined Upon console.logging siteDa ...

Perform a search within an iframe

Despite the fact that iframes are considered outdated, I am attempting to create a browser within an HTML browser. I have implemented a search bar that opens the typed input in a new window which searches Google. However, I would like to modify it so that ...

What is the best way to engage in conversations with several users using socket.io?

Currently, I am in the process of developing a chat application with authentication. The implementation involves socketio for real-time communication and jwt along with cookies for user authentication. The connection to the database has been successfully e ...

How to handle type errors when using properties in Vue3 Single File Components with TypeScript

I've hit a roadblock while attempting to utilize properties in Vue3. Despite trying various methods, I keep facing issues during the type-check phase (e.g.: yarn build). The project I'm working on is a fresh Vue3-ts project created using Vite. B ...

Get a collection of items from a separate object housed within the Firebase Database

After searching through numerous posts for a solution, I have come up short. I have two classes, a Recipe class: public class Recepta { private String nom; private List<Ingredient> ingredients; private List<String> pasos; public Recepta() { } ...

"Here's a cool trick: A guide on dynamically inserting a value into {% url tag %} using JavaScript within

Incorporating leaflet JS into a Django template, the aim is to display markers on a map where latitude and longitude are sourced from a queryset. Sending the queryset through views and utilizing the Django template language within <script> tags seeme ...

Utilizing Chart.js to visualize live data from a dynamic MySQL dataset

Struggling to create a dynamic dataset for my Chart.js project, specifically with the data obtained from getpromorevenuechart.php. Here's an example of the dataset: [{"mmyy":"2019-12","promocode":"promo1","amount":"2776"},{"mmyy":"2020-01","promocode ...

Is it possible to import multiple versions of a JavaScript file using HTML and JavaScript?

After extensive research, I am starting to think that using multiple versions of the same JavaScript library on a single page is not possible (without utilizing jquery Can I use multiple versions of jQuery on the same page?, which I am not). However, I wan ...

Having trouble with my sorting algorithm - am I overlooking something obvious?

function Controller($scope) { var sortItems = [ { "text": "Second", "a": 2 }, { "text": "Fifth", "a": 5 }, { "text": "First", "a": 1 }, { "text": "Fourth", "a": 4 }, { "text": "Third", "a": 3 } ]; va ...

Is it possible to transfer files using web-bluetooth technology?

As I work on developing an embedded system that counts the number of cars, saves their speed and time data in a logs file using rsyslog. Simultaneously, I am creating a web-API (in Typescript/Angular with Electron for Desktop usage and later Web as well) t ...

Iterating over a range of values with _.each()

Can someone help me figure out why my syntax is incorrect for applying two values from different iteratees (day.classes and event.part) on line 5? <div class="days"> <div class="headers"> <% _.each(daysOfTheWeek, function(day) { %&g ...

jQuery: What's the Difference Between Triggering a Click and Calling a Function?

Imagine having certain actions assigned to a link using .click or .live, and wanting to repeat the same action later without duplicating code. What would be the right approach and why? Option 1: $('#link').click(function(){ //do stuff }); //c ...

refreshing the webpage's content following the completion of an asynchronous request

I am working on an Ionic2 app that utilizes the SideMenu template. On the rootPage, I have the following code: export class HomePage { products: any = []; constructor(public navCtrl: NavController, public navParams: NavParams, private woo: WooCommer ...

Incorporating Information into an Array as a State Object within React.js

Hey everyone, I've hit a bit of a roadblock and I could really use some assistance. I'm trying to figure out why I keep getting an error when attempting to push user input into my array. Can someone take a look at my code and offer some insight? ...

Having trouble getting the CSS animation to work properly with the text

Update: The direct link is working fine, unlike the rocketscript version. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> I have been attempting to animate word rotati ...

The path NPM Package is missing in the package export

For my first NPM package, I've been diving into the manuals and exploring other projects for hours to make everything work. Getting Started with the Project The package successfully builds, generating files for ESM and CommonJS targets. It installs s ...