Retrieve data from an array within the user Collection using Meteor and React Native

I need assistance with retrieving the entire array [votes] stored within the User Collection. Below is the JSON structure

{
    "_id" : "pziqjwGCd2QnNWJjX",
    "createdAt" : ISODate("2017-12-21T22:06:41.930Z"),
    "emails" : [ 
        {
            "address" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483c2d3b3c083c2d3b3c662b2725">[email protected]</a>",
            "verified" : false
        }
    ]
    "votes" : [ 
        {
            "ZyYZ4LDTaeWNMN9eE" : "yes"
        },
        {
            "DSHhkdsjkdhsddsqd" : "no"
        }
    ]
}

Could someone provide guidance on how to display that array using console.log? I want to verify its existence before proceeding with an insertion operation.

Answer №1

To simply verify the presence of an array with elements:

const userInfo = Meteor.users.findOne(userID);
const hasInteractions = userInfo && userInfo.interactions && typeof(userInfo.interactions) === 'object' && userInfo.interactions.length;

Answer №2

In the scenario where you are retrieving a single document from a MongoDB collection, follow this code:

var user = Users.findOne({_id: your_given_id});

if(user && user.votes && user.votes.length){
    console.log(user.votes);
    return user.votes;
}
console.log('No Votes Found.');
return [];

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 could be causing the malfunction of material-UI Tabs?

My Material UI Tabs seem to have stopped working after a recent update. I suspect it may be related to the react-tap-event-plugin update I installed. Initially, I thought it was due to tab indexing but even using values like value='a', value=&apo ...

Adjusting the position of a stationary element when the page is unresponsive and scrolling

Managing a large web page with extensive JavaScript functionality can be challenging, especially when dealing with fixed position elements that update based on user scroll behavior. A common issue that arises is the noticeable jumping of these elements whe ...

Interested in transforming and showcasing dates within a MySQL database?

Here's a form with 10 JQuery UI date pickers set up: $(function() { $("#datepicker").datepicker({ minDate: 'today', maxDate: "+90D", showOn: "button", buttonImage: "images/calendar-new2.jpg", buttonImageOnly: true, dateFormat: "D, dd M, yy" ...

Trying out the fetch api with Jest in a React Component: A step-by-step guide

As a newcomer to test driven development, I stumbled upon a section that talked about testing/mocking a fetch API. However, I am facing issues while trying to write my own test. In order to practice this concept, I created a simple weather app where I atte ...

Encountered an unexpected forward slash while trying to parse JSON using

When passing a file, why does the `parseJSON` function fail? The file is stored in variable a and then I attempt to parse it using `parseJSON`. var a = "/android/contents/img/uploads/img_2A0.png"; var result = jQuery.parseJSON(a); The error being displa ...

Is it possible to import npm modules conditionally?

Here is the structure of my project: - workspace - customPackage - customIndex.js - myProject - index.js - myProject2 - index.js During development, I need to import the package from my local workspace like this: //index.js import some ...

Storing an Excel file with JavaScript: A comprehensive guide

I've been struggling to save an Excel file using Javascript, but I'm facing compatibility issues with different browsers. I initially tried using BASE64 along with data URL, which worked well in Chrome and Firefox but failed in IE and Safari. ne ...

What could be causing the unexpected behavior of angular.isNumber()?

I'm encountering an issue with AngularJS's angular.isNumber, as it doesn't seem to work with strings that represent numbers. Is there a mistake on my end? Would utilizing isNaN() be a better approach? angular.isNumber('95.55') == ...

Enhanced file uploading feature in Firefox 4 using AjaxForm

<form action="upload.aspx" enctype="multipart/form-data" id="ajaxUploadForm" method="post"> <input type="file" name="fileBase" id="fileBase"><input type="submit" value="send" /> </form> $( "#ajaxUploadForm" ).ajaxForm( { iframe: "t ...

Exploring Angular's Dependency Injection

How can I verify that a module has properly loaded its required dependencies? I've added ngAnimate to the module definition, but it doesn't appear to be functioning within the application when it runs. The environment I'm navigating is quite ...

The value of `this` from React Store is currently returning null

I'm facing an issue where the this keyword is coming back as null in my store within a React component. I suspect that binding might be necessary, but I'm not entirely sure. Additionally, I am utilizing React Router and have the component wrapped ...

a service that utilizes $http to communicate with controllers

My situation involves multiple controllers that rely on my custom service which uses $http. To tackle this issue, I implemented the following solution: .service('getDB', function($http){ return { fn: function(){ return $http({ ...

Detecting whether a browser is capable of supporting dark mode

One method to determine if dark mode is active is by using prefers-color-scheme: dark: const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; Is there a way to detect if a browser supports dark mode as well? (By "supports ...

What could be the reason behind meteor not functioning properly anymore? It seems that the following error is occurring: "throw new Error(' '+ modPath+ '.node` is missing. Try reinstalling `node-fibe"

The current setup on my system includes Meteor 1.3.2.4 and Node v4.4.3 with all projects failing to load and displaying an error message. Initially, the issue was related to a missing module called fibers, which I attempted to resolve by executing various ...

access the $request variable within a loop in Laravel 5.8

My objective is to handle multiple answer fields. The initial set of answers is stored in the answer0 array, followed by the answer1 array, and so forth. In order to achieve this, I am trying to access them using $request->answer0, $request->answer1, ...

Ways to apply Angular ng-options outside of a select element besides traditional use?

According to the official documentation, ng-options can be used on any element, not limited to just the select tag. This flexibility allows for enhanced customization of the selection UI, such as displaying thumbnail images or additional information beyond ...

The button text in Bootstrap 5 is black instead of white as shown in their demo

During the installation of Bootstrap 5, I encountered an issue where many of my buttons are displaying a black font instead of the expected white font as shown in the Bootstrap 5 Documentation For instance, the .btn-primary button on the official Bootstra ...

Utilizing Jquery's Selectable IDs in a repetitive manner

I'm a newbie to jQuery and I'm attempting to create two lists where each item can be selected. Check out my code: <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI s ...

Encountered an issue while trying to use Figma's spellchecker extension

Despite following the instructions in the readme carefully, I am facing difficulties running the Figma spellchecker extension. Executing npm run build goes smoothly. But when I try to run npm run spellcheck, I encounter an error message in the console: co ...

Is there a way to create separate arrays for even and odd numbers using the elements in a single array?

I am in the process of creating a program that takes input as numbers and categorizes them into even and odd arrays, displaying them accordingly. I am looking for advice on how to improve this program by replacing the even numbers in the odd array with 0. ...