Home > Software design >  ReferenceError: res is not defined in NodeJS
ReferenceError: res is not defined in NodeJS

Time:03-27

My NodeJS Code. I am getting correct result in console but page is not redirected to listings page

function displayCities(cname) {
    var pageData = {
      title: "Listings",
      cities: null
    };
    axios(
      //the request
      {
        url: "https://sandbox.repliers.io/listings?city="   cname,
        method: "GET",
        headers: {
          'Content-Type': 'application/json', 
          'REPLIERS-API-KEY': 'SKoKOGhEO42QzdkZ1cowKgLGm2mwm4'
        }
      }
    ).then(function (response){
      //on success do stuff
      console.log(response.data);
      pageData.cities = response.data; //store JSON results in pageData.cities (previously null)
      res.render("listings", pageData);
    }).catch(function (error){
      console.log(error);
    });
  }

CodePudding user response:

You have not passed the res object to the function.

To be able to access methods of the res object, you should add it to the function signature and give it to the function where you call it.

  • Related