Tim Zhukov-Khovanskiy
Tim Zhukov-Khovanskiy

Reputation: 500

Meteor collection find() does not retrieve any data

I'm working through a meteor tutorial : https://www.meteor.com/tutorials/blaze/collections

I have a collection defined,

Tasks = new Mongo.Collection("tasks");

I've added two items to it, one directly from meteor mongo command line, and another one using:

Tasks.insert({ text: "Testing JS", createdAt: new Date() });

Here are the results from running db.tasks.find() on the backend:

{ "_id" : ObjectId("559e9569abbb64fe1d5fd89a"), "text" : "Hello world!", "createdAt" : ISODate("2015-07-09T15:38:17.742Z") }
{ "_id" : "obRN8Rcssa9yJqXzA", "text" : "Testing JS", "createdAt" : ISODate("2015-07-09T17:00:13.285Z") }

But when I run Tasks.find({}); on the front end, I get empty result. It just gives me a long JSON but no data from the database.

Upvotes: 5

Views: 3697

Answers (3)

Amit Akolkar
Amit Akolkar

Reputation: 21

Did you import the .js file of collection in front-end .js ?

import { Tasks } from '/your_path_goes_here/filename_goes_here.js';

If not import it and try below,

  1. Add Publish like

    if (Meteor.isServer) { Meteor.publish('tasks', function task() { return Task.find({}); }); }

  2. Subscribe it in Front-End .js

    Meteor.subscribe('task');

Upvotes: 0

Christian Fritz
Christian Fritz

Reputation: 21384

Looking at your code, you are not publishing anything:

if (Meteor.isServer) {
  Meteor.publish("tasks", function () {
    console.log(Tasks.find());
  });   
}

needs to be

if (Meteor.isServer) {
  Meteor.publish("tasks", function () {
    console.log(Tasks.find());
    return Tasks.find();
  });   
}

or just remove it if you use autopublish.

Upvotes: 2

David Weldon
David Weldon

Reputation: 64342

In meteor, you can view the documents returned by a cursor by calling fetch on it. For example:

console.log(Tasks.find().fetch());

Upvotes: 3

Related Questions