是否可以用流星中的变量替换.find()查询?

时间:2021-12-14 15:53:57

Problem:

I am no expert in javascript / meteor syntax

我不是javascript / meteor语法方面的专家

What I want to achieve:

我想要实现的目标:

replace queries inside .find() with variable to make the code simpler

用变量替换.find()内的查询以使代码更简单

My Code:

Um.. this code basically searches through collections. It will find installed items Or installed items with certain name.

嗯..这段代码基本上搜索了集合。它将找到已安装的项目或具有特定名称的已安装项目。

Template.xxx.helpers({
  case_a: () => {
    if(A === true){
      var case1 = case_1.find({ isInstalled: true }).fetch();
      var case2 = case_2.find({ isInstalled: true }).fetch();            
    } else {
      var case1 = case_1.find({ "name": {$regex: (".*"+searchText+".*"), $options: 'i'}, isInstalled: true }).fetch();
      var case2 = case_2.find({ "name": {$regex: (".*"+searchText+".*"), $options: 'i'}, isInstalled: true }).fetch();
    }
    var ret = case1.concat(case2);
    return ret;
  }
});

What I would like to know:

我想知道的是:

proper syntax that assigned into query

分配给查询的正确语法

Template.xxx.helpers({
  case_a: () => {
    var query = {};
    if(A === true){
      query = { isInstalled: true };      
    } else {
      query = { "name": {$regex: (".*"+searchText+".*"), $options: 'i'}, isInstalled: true };
    }
    var case1 = case_1.find({ query }).fetch();
    var case2 = case_2.find({ query }).fetch();            
    var ret = case1.concat(case2);
    return ret;
  }
});

any help would be appreciated!!

任何帮助,将不胜感激!!

1 个解决方案

#1


2  

Since query is a object, it can be passed directly to the find() method.

由于query是一个对象,因此可以直接传递给find()方法。

case_1.find(query)

instead of

case_1.find({query})

#1


2  

Since query is a object, it can be passed directly to the find() method.

由于query是一个对象,因此可以直接传递给find()方法。

case_1.find(query)

instead of

case_1.find({query})