Meteor 流星发布/订阅
我创建了一个非常简单的示例,试图让我的头脑清醒过来,发布和订阅 我删除了: 自动发布 不安全 我的mongo数据库如下所示Meteor 流星发布/订阅,meteor,Meteor,我创建了一个非常简单的示例,试图让我的头脑清醒过来,发布和订阅 我删除了: 自动发布 不安全 我的mongo数据库如下所示 var Country = new Meteor.Collection("country"); if(Meteor.isClient) { Meteor.subscribe("country"); Template.test.country = function () { return Country.find(); };
var Country = new Meteor.Collection("country");
if(Meteor.isClient) {
Meteor.subscribe("country");
Template.test.country = function () {
return Country.find();
};
}
if(Meteor.isServer) {
Meteor.publish("country", function() {
return Country.find();
});
}
<head>
<title>test</title>
</head>
<body>
{{> test}}
</body>
<template name="test">
<p>{{country}}</p>
</template>
meteor:PRIMARY>db.country.find(){“\u id”:
ObjectId(“5332b2eca5af677cc2b1290d”),“国家”:“新西兰”,
“城市”:“奥克兰”}
我的test.js文件如下所示
var Country = new Meteor.Collection("country");
if(Meteor.isClient) {
Meteor.subscribe("country");
Template.test.country = function () {
return Country.find();
};
}
if(Meteor.isServer) {
Meteor.publish("country", function() {
return Country.find();
});
}
<head>
<title>test</title>
</head>
<body>
{{> test}}
</body>
<template name="test">
<p>{{country}}</p>
</template>
我的html文件如下所示
var Country = new Meteor.Collection("country");
if(Meteor.isClient) {
Meteor.subscribe("country");
Template.test.country = function () {
return Country.find();
};
}
if(Meteor.isServer) {
Meteor.publish("country", function() {
return Country.find();
});
}
<head>
<title>test</title>
</head>
<body>
{{> test}}
</body>
<template name="test">
<p>{{country}}</p>
</template>
测试
{{>测试}
{{国家}
我不明白为什么这行不通。我在服务器上发布,订阅它。我知道这不是我在实时环境中要做的事情,但我甚至不能复制检索整个集合以在客户端上查看
如果我更改此返回国。查找();返回Country.find().count();我得到1。但是,国家/地区文本不会出现
我很想知道发生了什么。我是新的开发和使用流星。我真的很喜欢这个框架
干杯一切正常。如果要打印出所有文档,必须使用“每个帮助器”:
<template name="test">
{{#each country}}
<p>{{country}}, {{city}}</p>
{{/each}}
</template>
{{每个国家}
{{国家},{{城市}
{{/每个}}
谢谢Peppe L-G,我稍微修改了我的.js文件,最终结果如下:
.js文件
var Country = new Meteor.Collection("country");
if(Meteor.isClient) {
Meteor.subscribe("country");
Template.test.countries = function () {
return Country.find();
};
}
if(Meteor.isServer) {
Meteor.publish("country", function() {
return Country.find();
});
}
html文件
<head>
<title>test</title>
</head>
<body>
{{> test}}
</body>
<template name="test">
{{#each countries}}
<p>{{country}}, {{city}}</p>
{{/each}}
</template>
测试
{{>测试}
{{{每个国家}
{{国家},{{城市}
{{/每个}}
既然我的代码几乎正确,为什么我不能用Country.findOne()查询控制台,或者通过键入Country查看集合?使这些数据在客户端上可用,我想我仍然能够从控制台进行查询,因为我没有实现任何方法
谢谢你的帮助
干杯如果您键入
Country.findOne()
,您在客户端控制台会得到什么?我想这可能是因为浏览器无法显示您通过模板.test.country
帮助程序返回的对象数组。我得到的国家没有定义,感谢您的回复。您的集合“存储”在变量country
中,该变量是js文件中的局部变量。如果要在文件外部使用,请创建一个全局变量(开始时删除var
)。