oracle发布和订阅,Meteor发布和订阅

删除自动发布

在这个例子中,我们将使用 PlayersCollection 集合有以下数据。如果不能确定Meteor中如何创建 MongoDB 集合,您可以查看我们的集合这一章。

018a429d4b3b87a71283b5564d1e1128.png

为了确保我们的数据,我们需要删除包自动发布,这是允许使用在客户端的数据。

C:\Users\Administrator\Desktop\meteorApp>meteor remove autopublish

在此步骤后,你会发现,无法从客户端获取数据库中的数据。只能在命令提示符窗口中的服务器端看到它。检出下面的代码 -

meteorApp/client/app.js

var PlayersCollection = new Mongo.Collection('playersCollection');

var myLog = PlayersCollection.find().fetch();

console.log(myLog);

命令提示符窗口将显示整个集合有四个对象,而开发者控制台会显示一个空数组。现在,我们的应用程序更安全。

7fa4f5ddd77250df49136aa1d537a530.png

使用发布和订阅

比方说,我们要允许客户端使用您的数据。要允许这一点,我们需要在服务器上创建 Meteor.publish()方法。该方法将数据发送到客户端。为了能够接收和使用在客户端的数据,我们将创建Meteor.subscribe()方法。在该示例的结尾,我们检索的数据库。这段代码可以在客户端和服务器上运行。

var PlayersCollection = new Mongo.Collection('playersCollection');

if(Meteor.isServer) {

Meteor.publish('allowedData', function() {

return PlayersCollection.find();

})

}

if (Meteor.isClient) {

Meteor.subscribe('allowedData');

};

Meteor.setTimeout(function() {

var myLog = PlayersCollection.find().fetch();

console.log(myLog);

}, 1000);

我们可以看到,数据会记录在开发者控制台和命令提示符窗口。

3d8224029b19e2dded796f5096e5b1c9.png

过滤客户端数据

我们还可以发布数据的一部分。在这个例子中,我们只发布 name="John"的数据。

var PlayersCollection = new Mongo.Collection('playersCollection');

if(Meteor.isServer) {

Meteor.publish('allowedData', function() {

return PlayersCollection.find({name: "John"});

})

}

if (Meteor.isClient) {

Meteor.subscribe('allowedData');

};

Meteor.setTimeout(function() {

myLog = PlayersCollection.find().fetch();

console.log(myLog);}, 1000);

当我们运行这段代码,命令提示符将记录所有的数据,而客户端控制台将只日志记录两个名称为 John 的对象。

7473d85194062dbc51ae3e090d09dccf.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值