2

Im new to meteor, I have a problem I can't get collection from mongodb (i use iron router )

/client/routes.js

Router.route('/page', function(){
  this.render('page');
});

/client/foo.js

city = new Mongo.Collection('data');

if (Meteor.isClient) {

  Template.foo.helpers({
    data: function(){
      return city.find();
    }
  });

}

client/views/foo.html

<template name="foo">
    {{#each data}}
        {{> all_data}}
    {{/each}}
</template>
<template name="all_data">
    <li>{{city}}</li>
</template>

in chrome console the command city.find() give me:

L…n.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: M…o.Matcher, skip: undefined…}

and in mongo console db.data.find() it work fine

i think there a problem to connect to mongodb

3
  • try a console.table (city.find().fetch()); in your client code or console. The find() method returns a cursor, the findOne() or find().fetch() returns an array of documents. Commented Jul 11, 2015 at 13:51
  • city.find().fetch() return just [] , and findOne() return undefined Commented Jul 11, 2015 at 13:55
  • If you removed the autopublish package, keep in mind that you need to define allow-deny rules and publications in order to access your data on the client side. See discovermeteor.com/blog/…. You also need to define your collection in a both folder, so it refers to the same collection both on server and client (not sure this is mandatory if names matches). Commented Jul 11, 2015 at 13:59

1 Answer 1

0

The collections need to be defined on both server and client side for autopublish to work -- files in /client are only executed on the client side, so city = new Mongo.Collection('data'); is unknown to the server.

Move the file

 /client/foo.js

into the parent directory

 /foo.js

And it will likely work

Sign up to request clarification or add additional context in comments.

2 Comments

Just tested it, and apart from that your template is called "foo" and the router is rendering "page", then it worked perfectly well for me. Change the name of the page you render to be the same as the template name, and put all the files in the top level directory before you start playing around with /client and /server
Also, you do not say that you have removed any of the packages, such as insecure or autopublish -- if you have your solutin would look differently. Ty to create a new project, just add meteor add iron:router and move the files to the top level directory of new project, and it should all work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.