Issue with collecting data in Meteor Collection

After making some edits to another file and increasing the complexity of my application, a tracker function that was previously working fine is now experiencing issues. It is no longer returning any data for

  1. vrLoan
  2. fundingData

This is a snippet of my code:

Tracker.autorun(()=>{ // Tracker function for reactivity
  const id = Session.get('data')._id;
  const loans = Loans.find({fileId: id});

  loans.forEach(o=>{
    const vrLoan = VRLoans.find({parentId: o._id});
    const fundingData = Funding.find({parentId: o._id});

    const t = o.Type;
    const n = o.description;

    if(t && n  && vrLoan.count() && fundingData.count()){
      console.log("here")
      fundDep.changed();
      fundingNameSpace[t] = {};

      if(t == "Bank Loan" || t == "Directors Loan" || t == "Lease" || t == "Loan Out/Invesment"){
        fundingNameSpace[t][n] = new LoanCalc(t, vrLoan, fundingData);
      }else if(t == "Share"){
        fundingNameSpace[t][n] = new ShareCapCalc(vrLoan, fundingData);
      }else if(t == "Other"){
        fundingNameSpace[t][n] = new OtherIncomeCalc(vrLoan, fundingData);
      }else if(t == "Cap"){
        fundingNameSpace[t][n] = new CapitalGrantCalc(vrLoan, fundingData);
      }
    });
  });

It seems that the additional code I implemented may have caused a slowdown in the query process, resulting in a length of 0 for the queries.

I would appreciate it if someone could help explain why this issue is occurring.

Answer №1

The initial if statement will not be executed as intended because you are checking the length of vrLoans and fundingData, which are MongoDB cursors rather than standard arrays.

Instead of using .length, you should use .count() to verify that they are not empty, or perform a .fetch() after your find() method to convert them into regular JavaScript arrays.

https://docs.mongodb.com/manual/reference/method/cursor.count/

(However, based on this mistake, it seems like this particular function may not have been functioning correctly prior to any recent modifications made to the application.)

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

What is causing my for/in loop to return null results, while the regular for loop works perfectly fine? (VISUALS included)

My goal is to iterate through an array of objects using a for/in loop in order to log specific properties of each object to the Chrome dev console. However, I am encountering issues as I keep getting null values. As a workaround, I attempted to use a regul ...

Unable to retrieve count data from MongoDB when using Node.js

I am looking for a query that can fetch the total count based on key value of a document by matching certain values from an array using MongoDB. Below, I have outlined my document and input data. dataArr=[ {'login_id':9937229853,'locati ...

Retrieve the unique payment ID generated from the database and present it on the frontend

Greetings, I am currently working on integrating a payment processor and I require the automated generation of the corresponding payment ID in the backend. On the frontend side, I have implemented JS to request data from the backend, but I'm facing ...

Unforeseen alterations in value occur in JavaScript when converting to JSON format

Having trouble generating a gantt chart from a JSON string, specifically with parsing the JSON string into a JSON object. I have a variable myString containing a JSON string that looks like this: {"c": [{"v": "496"}, {"v": "Task name 1"}, {"v": "9, "}, { ...

Error in JavaScript: Uncaught TypeError - Unable to access the "left" property of an undefined object

This error is really frustrating and I've come across several inquiries regarding this console issue. It's not providing enough information in the chrome console for me to effectively troubleshoot. /** * Adjustment of dropdown menu positio ...

When additional elements follow, the button ceases to function properly in JavaScript

I am working on creating a text-based idle game that involves multiple buttons and text around them. However, I have encountered an issue where the functionality stops working when I try to add text after the "Work" button. The callback function is no lon ...

Moving files by dragging and dropping rather than deleting them

I have successfully implemented a feature using JavaScript to drag and drop multiple files, followed by displaying those images. The code below is fully functional without any errors. Need help with: I am now looking to add the ability to remove these ima ...

How can I pass a PHP variable to a JavaScript variable using PHP and JQuery/JavaScript?

I am facing a challenge with a large <select> input that is used across multiple pages. My idea is to separate this dropdown box into its own PHP file and load it externally using JQuery. Is this approach feasible? Here's an outline of what I ha ...

combine ngClass properties if substitution is true

My directive includes replace: true in the definition. <my-custom-tag> </my-custom-tag> This is the template for the directive: <div data-ng-class="{'class1': condition1, 'class2': condition2}"> </div> When u ...

Upgrading to Bootstrap 5 and customizing the collapse button text upon click

I've spent a lot of time searching for a solution to my problem, but all the advice I found is for bootstrap 3 and 4, not version 5. Below is the code I am currently using: <div class="p-3">9 Members Online <a class="p-1 btn ...

No files located by the server

My experience with writing a basic express script to serve a webpage with embedded Javascript has been quite frustrating. The server seems to struggle finding the files I provide, and what's even more aggravating is that it sometimes works but then su ...

"Exploring the world of RGB color schemes in ThreeJS

I have a collection of points stored in a large string, with newline characters \n separating each point. Each point is saved as x y z r g b, where r g b values fall between 0 and 255. After reading through the ThreeJS documentation, I found a way to ...

Looking to remove a specific field from a URL with jQuery

I am trying to extract a specific tag from a string, like in this example: url = 'https://example.com/tag33?tag=17&user=user123'; The goal is to retrieve the value of the tag. If anyone has a solution or suggestion on how to achieve this, ...

What could be the reason for my array parameter not being included in the POST request

When working on my laravel 5.7 / jquery 3 app, I encountered an issue while trying to save an array of data. After submitting the form, I noticed that only the _token parameter is being sent in the POST request as seen in the browser console: let todos_co ...

Is it appropriate to include a function within a loop?

I'm curious to know if it's considered good practice to call a function inside a loop. Both of the following code snippets produce the same result, but I want to add clarity by using a function. Is this considered good practice? Thank you. Code ...

What is the best way to access and verify data from an array that has been passed through props

I am working with a component that takes an array as a prop <Component runners={['1','2']}/> Inside this functional component, my goal is to generate an element for each value in the array. Here's my logic: const { runners ...

Building an array of objects using a foreach loop

i am struggling to create an array of objects from two input groups, each group consists of 3 inputs with data-side attributes set to left or right every input has a class named "elm" and a data-pos attribute set to a, b, or c <input class="elm-left elm ...

Tips for eliminating duplicate entries in ag grid using Angular

Is there a way to eliminate the recurring assetCode entries in ag grid? The PRN and PRN1 values seem to be repeating unnecessarily. Check out the code below: list.component.ts ngOnInit() { this.rowData.push( { 'code': 'Machi ...

The node.js system automatically restarts an API call when a timeout occurs

Current Setup: I am using a combination of sails.js for the backend API and React for the frontend. The communication between the frontend and backend is handled by the fetch API. Scenario: Within some of my API endpoints, I need to run an external file ...

Apollo Client's useQuery function is causing unnecessary refetches when using Next.js' router.push method

Currently, I'm facing an issue where a query within a useQuery Apollo Client hook is being re-run unnecessarily every time Next.js's router.push function is triggered. The problem code snippet looks like this: const Parent = () => { useQuery ...