Unable to retrieve data from Meteor find query

I have a collection created in collections.js

portfolioItems = new Mongo.Collection('portfolioitems');

This collection is then subscribed to in subscriptions.js

Meteor.subscribe('portfolioitems');

And published in publications.js

Meteor.publish('portfolioitems',function(){
    return portfolioItems.find();
});

When I query Mongo from the server using db.portfolioitems.find(), it returns:

{ "_id" : "W9AeauCpMgPw2j5hf", "title" : "Commercial Design", "slug" : "commercial-design", "image" : "" }

However, when I try to find this item from the client or iron-router, the find method returns nothing. For example:

portfolioItems.find({'_id':'W9AeauCpMgPw2j5hf'});

Returns a LocalCollection.Cursor with undefined key values:

_selectorId: undefined

_transform: null

collection: LocalCollection

fields: undefined

limit: undefined

The findOne method works and returns the document:

portfolioItems.findOne({'_id':'W9AeauCpMgPw2j5hf'})

Object { "_id" : "W9AeauCpMgPw2j5hf", "title" : "Commercial Design", "slug" : "commercial-design", "image" : "" }

My challenge is returning all items with the same title. Using findOne() for this isn't suitable.

What could be the issue here?

Answer №1

The issue arose due to the discrepancies in iron-router's data object. My initial attempt with iron-router was:

waitOn:function(){

    return [Meteor.subscribe('projectsportfolio'),Meteor.subscribe('portfoliocategories')];

}
data:function(){
    currentSlug = this.params.category;
    currentCategory = projectsPortfolio.find({slug:currentSlug});
    if(typeof currentCategory != 'undefined'){
        return currentCategory
    }
}

However, this approach only resulted in a cursor output as pointed out by @David Weldon. The correct resolution is:

data:function(){
    currentSlug = this.params.category;
    currentCategory = projectsPortfolio.find({slug:currentSlug}).fetch();
    if(typeof currentCategory != 'undefined'){
        return currentCategory
    }
}

Now, currentCategory will be an array of objects that can be iterated through in my template like so:

{{#each currentCategory}}
    {{keyvalue}}
{{/each}}

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

Find the string "s" within a div element aligned vertically, using Popper

Currently utilizing Popper from Material-UI <Popper id={"simplePopper"} open={true} style={{backgroundColor: 'red',opacity:'0.5',width:'100%',height:'100%'}}> <div style={{height:"100%", ...

Reviewing and Implementing React and Material-UI Autocomplete for Selecting Multiple

Having trouble using Formik and MUI Autocomplete with multiple items. Specifically, when searching for "Pencil", it doesn't highlight the item. Also, you can select it twice by clicking on it. Desired outcome: Being able to select one or more items. ...

Dealing with errors in Next.js api: Best practices

When it comes to organizing code in Next.js api handlers, what is the most effective practice? In a video I watched, the presenter suggested placing all REST routes in two specific files: pages/api/users/index.ts would manage operations that do not requ ...

Retrieving values from nested arrays in Vue.js

I'm currently delving into Vue3. My goal is to extract the values from an array within an array in order to create a neat table. Once extracted, I plan to separate these values with commas. For more information, you can visit this link: https://stack ...

Adjusting the color of a box in Threejs through dat.gui.min.js controls

I want to enhance the user experience by allowing them to choose a color from the HEX menu and update the color of the box in the scene using UI controls. Below is the JavaScript code I have implemented for this purpose: // author: Arielle Mueller // date ...

Ways to showcase Switch/Case in two separate drop-down menus

I'm currently working on a PHP file with a switch/case structure that I believe is in JSON format. While I may not be an expert in PHP, AJAX, and JSON, I'm really putting effort to learn more about it. <?php switch($_GET['make'] ...

What is the best way to retrieve a previously used jQuery function?

The dynamic table I've created has the functionality to search, display search results in pagination, and auto-compute fields. This is how it works: Users search for their items in the search box If the search results are extensive, the system displ ...

A ReferenceError was thrown because angular is not defined within the angular-moment.js script

After spending an hour trying to figure out what went wrong, I still can't solve this problem. I've searched on stackoverflow for answers, but nothing seems to be helpful. The issue arises when trying to integrate Moment js into my project. Che ...

VueJS together with Firebase: The guide to password validation

I am currently working on a web form developed using VueJS that enables authenticated users to modify their passwords. The backend system relies on Firebase, and I am facing a challenge in validating the user's current password before initiating the p ...

I'm experiencing issues with event.preventDefault() not functioning properly when used within a contenteditable div

I'm currently working with some basic Angular 7.x code that involves a contenteditable div. I'm attempting to prevent the default action when a user hits the [ENTER] key, but no matter what I do, it still moves the cursor to the next line. What a ...

What's the best way to manage the onClick event for individual input submit types using jQuery?

As a novice in jQuery, I am still in the learning phase. However, I urgently require a solution to set up a click event registration for each button within my table. The table below is created using GridView: <div id="gridView"> &l ...

Employing the `instanceof` operator on instances generated by constructors from complex npm dependencies

Context: In one of my npm modules, I've implemented error handling code with a custom error type called CustomError: function CustomError () { /* ... */ } CustomError.prototype = Object.create(Error.prototype); CustomError.prototype.constructor = Cu ...

Open boxes with walls that don't create shadows

Currently, I am facing an issue with an open-sided box created using MeshStandardMaterial and BoxGeometry. I have configured the box to cast and receive shadows, but it is not behaving as expected. I anticipated the interior of the box to darken when the p ...

Creating an AJAX URL in an external JavaScript file within a Django project

How can I verify if a student user's email exists in the database using keyup event in a registration form, and prevent form submission if the email is already registered? Below are the relevant files for achieving this: urls.py urlpatterns = [ ...

In PHP forms, ensure that only completed textboxes are submitted and empty textboxes are discarded

I've created a form that displays all available products from a database along with their prices. Users can input the quantity they want to purchase for each product, and upon submission, the total amount will be calculated. There are 5 products in th ...

Creating a recursive function using NodeJS

This particular challenge I am tackling is quite intricate. My objective is to develop a recursive function in NodeJS that can interact with the database to retrieve results. Based on the retrieved data, the function should then recursively call itself. F ...

Struggling to implement a vertical scroll bar in HTML code?

<body ng-app="myApp" ng-controller="myCtrl"> <div ng-show = "dataFromRest" ng-repeat = "x in dataFromRest.posts" > <div class="tittle" style="width: 25%;"> <a href="" ng-click="showDi ...

Conserving node.js native imports for Electron with rollup

I am working on a project using Electron, Svelte, and Typescript. Initially, I used a specific template from here, but it restricted access to node.js built-in imports like fs for security reasons in the browser/electron frontend. However, I do not requir ...

Adjust the size of the text within the div element as needed

Here is my code on jsFiddle where I am dynamically changing the text font to fit in the div. Everything is working perfectly fine. Currently, the text is within a span element, but I would like to remove the span and have the same functionality. However, w ...

Tips for importing a .geojson document in TypeScript using webpack?

I am trying to extract data from a .geojson file but faced some challenges while attempting two different methods: const geojson = require('../../assets/mygeojson.geojson'); The first method resulted in an error message stating: Module parse f ...