【发布时间】:2014-02-25 17:23:16
【问题描述】:
我是 Meteorjs 的新手,我正在尝试从现有的 MongoDB 中检索数据。 这是我到目前为止所拥有的:
我将环境变量 MONGO_URL 设置为 mongoDB url export MONGO_URL="mongodb://username:password@address:port/dbname"
-
使用以下代码创建了一个新的流星项目:
MyCollection = new Meteor.Collection('mycollection'); if (Meteor.isClient) { //Meteor.subscribe("mycollection"); console.log(MyCollection.findOne()); Template.hello.greeting = function () { return MyCollection.findOne(); }; } if (Meteor.isServer) { Meteor.startup(function () { // code to run on server at startup console.log(MyCollection.findOne()); }); }
我知道服务器端 console.log(MyCollection.findOne()); 在终端上打印出正确的数据时工作。
问题出在客户端。当我在浏览器上查看页面时,数据为空白,console.log(MyCollection.findOne()); 显示“未定义”。
我知道自动发布已打开,我不必从服务器端手动发布集合。
我想知道如何让客户端直接从我的外部 mongoDB 中读取。 如果您有任何建议,请告诉我!
【问题讨论】:
标签: javascript mongodb meteor