我有两个函数。当我从第二个函数调用第一个函数promise时,它会正确地打印输出,但我在第二个功能中的"newdata"变量返回了未定义的值。请帮忙。
我的功能是:
async keycloaktokennew(data: any):Promise<any>
{
return await this.httpService.axiosRef.post(
`http://localhost:8080/auth/realms/master/protocol/openid-connect/token`,
querystring.stringify({
username: 'stdev', //gave the values directly for testing
password: 'admin123',
grant_type: 'password',
client_id: 'admin-cli',
}),
{
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}
).then(function(response) {
console.log(response.data);
});
}
async newKeyToken(data: any){
const newdata = await this.keycloaktokennew(data);
if (newdata=="undefined") {
throw new BadRequestException(INVALID_CREDENTIALS);
}
else{
console.log("-------------------this is Result needed var-----------------");
console.log(newdata);
console.log("-------------------this is Result needed var-----------------");
return newdata;
}
}
if (newdata=="undefined")
应该是(!newdata)
,因为在当前流中,它期望从上一个函数undefined
传入"undefined"
,因为您不会从回调返回任何信息。
).then(function(response) {
console.log(response.data);
});
此外,您应该返回您的承诺中的数据。
).then(function(response) {
console.log(response.data);
return response.data
});
您需要返回响应
then(function(response) {
console.log(response.data);
return response.data;
});
61
控制台将打印计算表达式的结果。评估console.log((的结果是未定义的,因为console.log没有显式返回某些内容。它有打印到控制台的副作用。