Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Meteor:将Mongo选择器从客户端传递到服务器的最佳方法_Javascript_Mongodb_Meteor - Fatal编程技术网

Javascript Meteor:将Mongo选择器从客户端传递到服务器的最佳方法

Javascript Meteor:将Mongo选择器从客户端传递到服务器的最佳方法,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我有一个mongo集合,如下所示(Foo(X)=键;bar==值):EDIT-我来自关系数据库背景。很明显,我的收藏看起来不像下面的,但你明白了 +--------+--------+--------+ | Foo1 | Foo2 | Foo3 | +--------+--------+--------+ | Barbar | Barbar | Bar | | bar | Bar | BarBar | | Bar | barbar | barBar | |

我有一个mongo集合,如下所示(Foo(X)=键;bar==值):EDIT-我来自关系数据库背景。很明显,我的收藏看起来不像下面的,但你明白了

+--------+--------+--------+
|  Foo1  |  Foo2  |  Foo3  |
+--------+--------+--------+
| Barbar | Barbar |   Bar  |
|   bar  |  Bar   | BarBar |
|   Bar  | barbar | barBar |
|   ...  |   ...  |   ...  |
允许我的客户过滤数据对我来说很重要。有时,所有列都会有一个过滤器,其他时候,没有任何列会有过滤器,或者介于两者之间。目前,我正在处理以下问题:

客户端

Var aFoo1Filter = ["bar"]
Var aFoo2Filter = ["Barbar", "BarBar"]
Var aFoo3Filter = ["barbar", "bar"]
//Where the user can affect the array through some other code
Var sFilter = "Foo1: {$in: [\"bar\"]},
               Foo2: {$in: [\"Barbar\", \"BarBar\"]},
               Foo3: {$in: [\"barbar\", \"bar\"]}"
var oFilter = {
  Foo1: "bar"
}
服务器

Meteor.publish("foos", function (aFoo1Filter, aFoo2Filter, aFoo3Filter ) {
  return FooCl.find({Foo1: {$in: aFoo1Filter},
                     Foo2: {$in: aFoo2Filter},
                     Foo3: {$in: aFoo3Filter}}, 
                    {limit: 10});
});
Meteor.publish("foos", function (sFilter) {
  return FooCl.find({sFilter}, 
                    {limit: 10});
});
Meteor.publish("foos", function (oFilter) {
  return FooCl.find({oFilter}, 
                    {limit: 10});
});
我希望通过将一个对象或一个字符串从客户端传递到服务器来简化这一过程,但这两种尝试都没有成功。请参阅下面我的尝试:

尝试#1-通过字符串

客户端

Var aFoo1Filter = ["bar"]
Var aFoo2Filter = ["Barbar", "BarBar"]
Var aFoo3Filter = ["barbar", "bar"]
//Where the user can affect the array through some other code
Var sFilter = "Foo1: {$in: [\"bar\"]},
               Foo2: {$in: [\"Barbar\", \"BarBar\"]},
               Foo3: {$in: [\"barbar\", \"bar\"]}"
var oFilter = {
  Foo1: "bar"
}
服务器

Meteor.publish("foos", function (aFoo1Filter, aFoo2Filter, aFoo3Filter ) {
  return FooCl.find({Foo1: {$in: aFoo1Filter},
                     Foo2: {$in: aFoo2Filter},
                     Foo3: {$in: aFoo3Filter}}, 
                    {limit: 10});
});
Meteor.publish("foos", function (sFilter) {
  return FooCl.find({sFilter}, 
                    {limit: 10});
});
Meteor.publish("foos", function (oFilter) {
  return FooCl.find({oFilter}, 
                    {limit: 10});
});
//////////////////

尝试#2-穿过一个物体

Var aFoo1Filter = ["bar"]
Var aFoo2Filter = ["Barbar", "BarBar"]
Var aFoo3Filter = ["barbar", "bar"]
//Where the user can affect the array through some other code
Var sFilter = "Foo1: {$in: [\"bar\"]},
               Foo2: {$in: [\"Barbar\", \"BarBar\"]},
               Foo3: {$in: [\"barbar\", \"bar\"]}"
var oFilter = {
  Foo1: "bar"
}
客户端

Var aFoo1Filter = ["bar"]
Var aFoo2Filter = ["Barbar", "BarBar"]
Var aFoo3Filter = ["barbar", "bar"]
//Where the user can affect the array through some other code
Var sFilter = "Foo1: {$in: [\"bar\"]},
               Foo2: {$in: [\"Barbar\", \"BarBar\"]},
               Foo3: {$in: [\"barbar\", \"bar\"]}"
var oFilter = {
  Foo1: "bar"
}
服务器

Meteor.publish("foos", function (aFoo1Filter, aFoo2Filter, aFoo3Filter ) {
  return FooCl.find({Foo1: {$in: aFoo1Filter},
                     Foo2: {$in: aFoo2Filter},
                     Foo3: {$in: aFoo3Filter}}, 
                    {limit: 10});
});
Meteor.publish("foos", function (sFilter) {
  return FooCl.find({sFilter}, 
                    {limit: 10});
});
Meteor.publish("foos", function (oFilter) {
  return FooCl.find({oFilter}, 
                    {limit: 10});
});

我现在没有带我的机器,所以我不能提供更多关于抛出错误类型的细节。希望今晚会有更多的信息。谢谢你的帮助

解决此问题的最简单方法是使用选择器进行订阅:

客户 服务器 但是,重要的是要认识到,这使客户能够从
FooCl
集合中请求所需的任何文档。一个改进的解决方案是通过在选择器上使用来限制可以请求的内容。例如:

Meteor.publish('foos', function(selector) {
  check(selector, {
    Foo1: Match.Optional({$in: [String]}),
    Foo2: Match.Optional({$in: [String]})
  });

  if (!_.isEmpty(selector)) {
    return FooCl.find(selector, {limit: 10});
  }
});

这将确保
选择器
在向客户发送任何文档之前符合可接受的模式。

这就做到了,谢谢!看起来我的错误是在我的sFilter和oFilter周围放了一个大括号{}。另外,感谢您提供有关选择器上匹配的提示!