Meteor cannot get data

流过昼夜 提交于 2020-01-04 14:21:31

问题


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:


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



来源:https://stackoverflow.com/questions/31357837/meteor-cannot-get-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!