1
我在javascript /流星語法方面的專家
我想達到的目標:
更換內部.find()與可變查詢簡化代碼
我的代碼:
這個代碼基本上搜索集合。它會查找已安裝的項目或具有特定名稱的已安裝項目。
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;
}
});
我想知道些什麼:即分爲查詢
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;
}
});
任何幫助,將不勝感激
正確的語法!