ReferenceError: res is not defined in NodeJS
ReferenceError: res is not defined in NodeJS
我的 NodeJS 代码。我在控制台中得到正确的结果,但页面没有重定向到列表页面
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);
});
}
您还没有将 res
对象传递给函数。
为了能够访问 res
对象的方法,您应该将其添加到函数签名中并将其提供给您调用它的函数。
我的 NodeJS 代码。我在控制台中得到正确的结果,但页面没有重定向到列表页面
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);
});
}
您还没有将 res
对象传递给函数。
为了能够访问 res
对象的方法,您应该将其添加到函数签名中并将其提供给您调用它的函数。