Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Data from Meteor Collections

Tags:

meteor

I'm having a few problems when trying to get data from a Meteor Collection and I need some advice.

The collection has been defined, published, and subscribed successfully. If I send the data to a template, it displays fine:

Template.Lists.Projects = function(){
    return Projects.find();
};

But I am trying to use the data before displaying it, and this is where I run into problems. First, I'm getting some inconsistencies between find() and findOne(). find(selector) works fine and returns a cursor, but findOne(selector) returns "undefined." I really am only looking for 1 entry, so find() seems unnecessary.

Returns LocalCollection.Cursor:

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);

Returns undefined:

var find = Projects.findOne({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);

My next problem arises when using .fetch() on the LocalCollection.Cursor. It returns an empty array.

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch);

all this returns is the following line:

[ ]

When I try to specify a specific key from the array I want to display, like:

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch.name);

It returns undefined.

I'm still familiarizing myself with Meteor and have never used MongoDB (or minimongo), so I'm probably just making some stupid mistake. If anyone can point it out to me I would be thrilled!

like image 242
Alex Getty Avatar asked Dec 18 '12 20:12

Alex Getty


People also ask

What is Meteor collection?

Collections are Meteor's way of storing persistent data. The special thing about collections in Meteor is that they can be accessed from both the server and the client, making it easy to write view logic without having to write a lot of server code.

What is Meteor database?

The Video Meteor Database is currently kept on the IMONET Homepage and contains video data of over 2.6 Mio single station meteors recorded in 630,000 hours effective observing time spread over 5,700 nights. The oldest records date back to 1993.

How do I link my meteor to MongoDB?

Open a terminal window and run meteor command. It will start running on localhost:3000 if you have not changed to port. Go to Robomongo (or your favorite mongodb client software) and create a new connection, making sure to change the connection address to localhost and the given the port number.

How do I find the schema of a collection in MongoDB?

We can get the schema object/first document of the collection using : var schemaObj = db. users. findOne();


2 Answers

Your results for find() and findOne() are consistent. Basically, Mongo or minimongo is simply not finding a document that matches that _id. FindOne() is precisely like doing a find(selector, options).fetch()[0].

Your Lists.Projects template is likely expecting a collection, array or hash it can iterate over. You cannot return one specific document. If you are using {{#each Projects}} you must provide some way for the template to iterate not just a single value.

like image 102
David Wihl Avatar answered Sep 22 '22 15:09

David Wihl


I recently had the same problem, The collection find() returned nothing when used from a query.observe.

The problem was the order of the subscribe of collections.

For example if you have a collection called Lists and one called Projects,

If you're getting the projects by observing a query on lists, and you had :

Meteor.subscribe('Lists');
Meteor.subscribe('Projects');  

What happens is the query observe trigger is called, but the Projects are not yet fetched from the server. So Projects.find().fetch().length = 0.

To fix it, simply do

Meteor.subscribe('Projects');    
Meteor.subscribe('Lists');
like image 41
Dany Y Avatar answered Sep 21 '22 15:09

Dany Y