Finding collection Meteor () does not retrieve data

I am working on a meteorite tutorial: https://www.meteor.com/tutorials/blaze/collections

I have a specific set,

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

      

I added two items to it, one from the meteor mongo command line and the other:

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

      

Below are the results of running db.tasks.find () on the server:

{ "_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 ({}); at the front end, I get an empty result. It just gives me long JSON, but no data from the database.

+3


source to share


3 answers


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



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

      

+3


source


Looking at your code, you are not posting anything:

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

      

should be



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

      

or just remove it if you are using autopublish.

+2


source


Have you imported the collection .js file in the .js frontend?

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

      

If not import it and try below,

  • Add Publish As

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

  • Subscribe to Front-End.js

    Meteor.subscribe('task');

0


source







All Articles