Meteor cannot get data

2019-07-23 15:57发布

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

1条回答
姐就是有狂的资本
2楼-- · 2019-07-23 16:44

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

查看更多
登录 后发表回答