Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.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 有没有一种方法可以将这一承诺转变为“承诺”呢;“最佳编码实践”;版本_Javascript_Node.js_Mongoose_Architecture - Fatal编程技术网

Javascript 有没有一种方法可以将这一承诺转变为“承诺”呢;“最佳编码实践”;版本

Javascript 有没有一种方法可以将这一承诺转变为“承诺”呢;“最佳编码实践”;版本,javascript,node.js,mongoose,architecture,Javascript,Node.js,Mongoose,Architecture,我的代码运行良好,我只想继续用良好的编码实践解决这个搜索特性。我觉得我重复同样的代码太多次了。我有两种不同类型的对象“programs”和“rentals”,如果用户在搜索菜单中选择了这两种对象中的一种,我只想在数据库中搜索指定的对象,但是如果用户同时选择了这两种对象或两者都没有,我想执行这两种搜索功能。 为了实现这一点,我有一个条件if来检查这些搜索选项,如果只选择了一个,我将在相应的模式上执行相应的find() if((queryObj.selectedSearchObjectType.in

我的代码运行良好,我只想继续用良好的编码实践解决这个搜索特性。我觉得我重复同样的代码太多次了。我有两种不同类型的对象“programs”和“rentals”,如果用户在搜索菜单中选择了这两种对象中的一种,我只想在数据库中搜索指定的对象,但是如果用户同时选择了这两种对象或两者都没有,我想执行这两种搜索功能。 为了实现这一点,我有一个条件if来检查这些搜索选项,如果只选择了一个,我将在相应的模式上执行相应的find()

if((queryObj.selectedSearchObjectType.indexOf("rentals") > -1))
  {
     let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
               getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
           ));
      searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
   }
然而,正如您可能注意到的那样,为了实现这一点,我重复了相同的代码4次,我想进行良好的编码实践,因此我认为这看起来很难看,也不有效

if(queryObj.title) //If we have a title seacrh
        {
            queryObj.title = queryObj.title.trim();
            const titleSplited = queryObj.title.split(' ');//transform it to an array

            if(titleSplited.length > 0)
            {
                // Check if selectedSearchObjectType is defined to execute queryes selectively
                if(queryObj.selectedSearchObjectType)
                {
                    // Check if selectedSearchObjectType contains 'rentals' in order to add the result to finalResults Array 
                    if((queryObj.selectedSearchObjectType.indexOf("rentals") > -1))
                    {
                        let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
                            getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
                        ));
                        searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
                    }

                    // Check if selectedSearchObjectType contains 'programs' in order to add the result to finalResults Array 
                    if((queryObj.selectedSearchObjectType.indexOf("programs") > -1))
                    {
                        let foundProgramsByTitle = await Promise.all(titleSplited.map((value) =>
                            getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
                        ));
                        searchResults.programs = searchResults.programs.concat(foundProgramsByTitle);
                    }
                }else{
                    //If no selectedSearchObjectType was selected then execute both
                    let foundProgramsByTitle = await Promise.all(titleSplited.map((value) =>
                        getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
                    ));
                    let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
                        getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
                    ));
                    searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
                    searchResults.programs = searchResults.programs.concat(foundProgramsByTitle);
                }                    

             // Filter results code ...
             // .........................
             // End of Function               
        }

关于如何将其重构为更好的版本,有什么想法吗?

可以是这样的:

if(!queryObj.title) return; // just exit from the function or return representation of an empty result


queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array

if(titleSplited.length <= 0) return; // same as above

// define the holder for search operations
const searchPromises = {};

if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf("rentals") > -1)
{
  searchPromises.rentals = Promise.all(titleSplited.map((value) =>
    getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
  ));
}

if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf("programs") > -1)
{
  searchPromises.programs = Promise.all(titleSplited.map((value) =>
    getProgramsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
  ));
}

if (searchPromises.rentals) {
  searchResults.rentals = searchResults.rentals.concat(await searchPromises.rentals);
}

if (searchPromises.programs) {
  searchResults.programs = searchResults.programs.concat(await searchPromises.programs);
}

// Filter results code ...
// .........................
// End of Function
因此,您可以将功能更改为:

if(!queryObj.title) return; // just exit from the function or return representation of an empty result

queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array

if(titleSplited.length <= 0) return; // same as above

// define the holder for search operations
const searchPromises = {};
const searchTypes = [RENTALS, PROGRAMS];

searchTypes.forEach((type) => {
  if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf(type) > -1)
  {
    searchPromises[type] = Promise.all(titleSplited.map(searchFns[type]));
  }
});

// iterate over created promises
for (let type in searchPromises) {
  searchResults[type] = searchResults[type].concat(await searchPromises[type])
}

// Filter results code ...
// .........................
// End of Function
if(!queryObj.title)返回;//只需退出函数或返回空结果的表示
queryObj.title=queryObj.title.trim();
const titleSplited=queryObj.title.split(“”)//将其转换为数组
如果(标题分割长度){
如果(!queryObj.selectedSearchObjectType | | queryObj.selectedSearchObjectType.indexOf(type)>-1)
{
searchPromises[type]=Promise.all(titleSplited.map(searchFns[type]);
}
});
//迭代创建的承诺
for(让输入搜索承诺){
searchResults[type]=searchResults[type].concat(等待搜索承诺[type])
}
//筛选结果代码。。。
// .........................
//功能结束

错误的站点-我甚至不知道这个存在!!!非常感谢你!哇,太谢谢你了!!!我开始担心性能问题,但这让我更好地理解了我必须做的事情:)谢谢!
if(!queryObj.title) return; // just exit from the function or return representation of an empty result

queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array

if(titleSplited.length <= 0) return; // same as above

// define the holder for search operations
const searchPromises = {};
const searchTypes = [RENTALS, PROGRAMS];

searchTypes.forEach((type) => {
  if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf(type) > -1)
  {
    searchPromises[type] = Promise.all(titleSplited.map(searchFns[type]));
  }
});

// iterate over created promises
for (let type in searchPromises) {
  searchResults[type] = searchResults[type].concat(await searchPromises[type])
}

// Filter results code ...
// .........................
// End of Function