我想在javascript中将值从一个类存储到另一个类。 我正在使用以下代码,
const suggestion=require('../helper/suggestion.js');
const constants = require('../constants/constants.json');
const chai = require('chai')
chaiHttp = require('chai-http');
chai.use(chaiHttp);
class publicHoliday
{
getSingleDateHoliday(airPortCode)
{
try
{
chai.request(constants.apiURL)
.get("/suggestions")
.query({airportIc: airPortCode})
.end(function(err,res)
{
for(var i=0;i<res.body.suggestions.length;i++)
{
suggestion.name=res.body.suggestions[i].name;
suggestion.holDates=res.body.suggestions[i].holDates;
suggestion.fromDate=res.body.suggestions[i].fromDate;
suggestion.toDate=res.body.suggestions[i].toDate;
}
});
}
catch(exception)
{
console.log("exception Is"+exception);
}
console.log(suggestion.name) //I want to print the name from API Here but is showing as "Undefined"
}
}
module.exports= new publicHoliday();
我正在使用以下类存储来自我们从publicHoliday类的API响应中获取的“名称”,“ holDates”,“来自”和“至”属性的值。
class suggestion
{
constructor()
{
var name=new Array();
var holDates=new Array();
var fromDate=new Array();
var toDate=new Array();
}
}
module.exports= new suggestion();
仅供参考:-我正在正确调用类,但是在执行代码后,它向我显示错误为“未定义”
我是JavaScript新手,有人可以让我知道我在哪里出错吗?