未在回调函数中定义对象

时间:2020-04-14 03:04:38

标签: node.js express

用户可以从下拉菜单中选择城市和所需的卧室数量。如果用户从菜单中选择一项或两项,则搜索应该可以进行。搜索菜单将针对房地产应用。运行代码时,出现错误消息“ ReferenceError:未定义cityObj”。如何解决此错误消息。

router.get("/", function(req, res){
  //function to search by city
  citySearch(req, res);

  //manage the object responses
  res.render(cityObj);

  //function to search by bedrooms
  bedroomSearch(req, res);

  //manage the object responses
  res.render(bedObj);
});

//search functions
function citySearch(req, res) {
  var noMatch = null;
    if(req.query.search) {
        const regex = new RegExp(escapeRegex(req.query.search), 'gi');
        // Get all homes from DB
        Home.find({city: regex}, function(err, allHomes){
           if(err){
               console.log(err);
           } else {
              if(allHomes.length < 1) {
                  noMatch = "No homes match that query, please try again.";
              }
         var linkRoute = "homes/index";
         var cityObj = {linkRoute:"homes/index", homes:"homes:allHomes", noMatch:"noMatch: noMatch"};
         //return object instead of res.render statement
         return cityObj; 
              //res.render("homes/index",{homes:allHomes, noMatch: noMatch});
           }
        });
    } else {
        // Get all homes from DB
        Home.find({}, function(err, allHomes){
           if(err){
               console.log(err);
           } else {
         var linkRoute = "homes/index";
         var linkObj = {linkRoute:"homes/index", homes:"homes:allHomes", noMatch:"noMatch: noMatch"};
         //return object instead of res.render statement
         return cityObj; 
              //res.render("homes/index",{homes:allHomes, noMatch: noMatch});
           }
        });
    }

}
// end of city search function

function bedroomSearch(req, res) {
  var noMatch = null;
    if(req.query.bed) {
        const regexBed = new RegExp(escapeRegex(req.query.bed), 'gi');
        // Get all homes from DB
        Home.find({bedrooms: regexBed}, function(err, allHomes){
           if(err){
               console.log(err);
           } else {
              if(allHomes.length < 1) {
                  noMatch = "No homes match that query, please try again.";
              }
         var linkRoute = "homes/index";
         var bedObj = {linkRoute:"homes/index", homes:"homes:allHomes", noMatch:"noMatch: noMatch"};
         //return object instead of res.render statement
         return bedObj; 
              //res.render("homes/index",{homes:allHomes, noMatch: noMatch});
           }
        });
    } else {
        // Get all homes from DB
        Home.find({}, function(err, allHomes){
           if(err){
               console.log(err);
           } else {
         var linkRoute2 = "homes/index";
         var bedObj = {linkRoute2:"homes/index", homes:"homes:allHomes", noMatch:"noMatch: noMatch"};
         //return object instead of res.render statement
         return bedObj; 
              //res.render("homes/index",{homes:allHomes, noMatch: noMatch});
           }
        });
    }

}


1 个答案:

答案 0 :(得分:0)

export const StepWrapper = styled.div<{ pos: Currentness, isCurrent: boolean }>({ width: '100%', height: '100px', display: 'flex', justifyContent: 'flex-start', alignItems: 'center', transition: 'font-size 0.75s ease', }, ({ pos, isCurrent }) => ({ fontWeight: isCurrent ? 'bold' : 'normal', fontSize: isCurrent ? 'x-large' : 'medium', top: pos * 100 + '%', position: pos === Currentness.Current ? 'relative' : 'absolute' })) 行上有错误,正在返回未定义的40,应该cityObj在该范围中定义。

尝试一下。

return linkObj