函数显示未定义的值,但在控制台中显示结果
Function is showing undefined value but in console its showing result
我有两个函数,一个是主函数,另一个是 api 调用函数,我在这个函数中调用 Api 实际上它在控制台中显示结果但是当我调用它时主函数内的函数然后显示 undefined
值。
下面是我的代码:
async mainFun(){
this.logger.log("in main function");
const dat = await this.apiFun();
this.logger.warn(dat); // Here its showing undefined
}
async apiFun = () =>{
const url = 'https://reqres.in/api/unknown';
axios.get(url).then((response) => {
this.logger.log("In nameFun " + response.data.total);
return response.data.total;
}).catch((error) => {
this.logger.log(error);
});
}
有人告诉我为什么显示 undefined
值。
async mainFun(){
this.logger.log("in main function");
const dat = await this.apiFun();
this.logger.warn(dat); // Here its showing undefined
}
async apiFun = () =>{
const url = 'https://reqres.in/api/unknown';
const request = await axios.get(url);
return request.data.total;
}
我有两个函数,一个是主函数,另一个是 api 调用函数,我在这个函数中调用 Api 实际上它在控制台中显示结果但是当我调用它时主函数内的函数然后显示 undefined
值。
下面是我的代码:
async mainFun(){
this.logger.log("in main function");
const dat = await this.apiFun();
this.logger.warn(dat); // Here its showing undefined
}
async apiFun = () =>{
const url = 'https://reqres.in/api/unknown';
axios.get(url).then((response) => {
this.logger.log("In nameFun " + response.data.total);
return response.data.total;
}).catch((error) => {
this.logger.log(error);
});
}
有人告诉我为什么显示 undefined
值。
async mainFun(){
this.logger.log("in main function");
const dat = await this.apiFun();
this.logger.warn(dat); // Here its showing undefined
}
async apiFun = () =>{
const url = 'https://reqres.in/api/unknown';
const request = await axios.get(url);
return request.data.total;
}