我正在尝试显示一个可过滤的客户端数据库,该数据库仅加载所需的内容。我有两个变量(从下拉列表中选择)设置两个 session (country_plugin和vertical_plugin)。然后我想显示满足这些要求的内容。
如果您不担心自动发布,下面的代码可以完美运行,但我不知道如何通过 pub/sub 实现相同的目的。
那么,简而言之,我如何在这里使用 session.get 和 if 语句?
filteredclients: function () {
var clientVerticalPicked = Session.get('vertical_plugin');
var clientCountryPicked = Session.get('country_plugin');
if (Session.get("country_plugin") === "none"){
return Clients.find({dealVerticalLink: clientVerticalPicked}, {sort: {dealMRR: -1}, limit:10});
} else if (Session.get("vertical_plugin") === "none"){
return Clients.find({dealCountry: clientCountryPicked}, {sort: {dealMRR: -1}, limit:10});
} else {
return Clients.find({$and:[{dealVerticalLink: clientVerticalPicked},{dealCountry: clientCountryPicked}]}, {sort: {dealMRR: -1}, limit:10});
}
}
最佳答案
session 变量仅在客户端上定义,因此您无法从发布函数中访问它们。您可以在客户端上的 Tracker.autorun 中进行订阅,并将 session 变量传递到发布函数中:
Tracker.autorun(function() {
Meteor.subscribe('filteredclients', Session.get('vertical_plugin'), Session.get('country_plugin'))
});
在服务器上:
Meteor.publish('filteredclients', function(vertical_plugin, country_plugin) {
if(country_plugin === "none") { return ... }
});
等等。那么客户端上的 Clients.find() 应该只包含您想要的记录。
关于javascript - 在发布/订阅中使用 session.get 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876527/