Json 从Angular4中的[object]获取值
这是我最后一个问题的代码:Json 从Angular4中的[object]获取值,json,angular,typescript,Json,Angular,Typescript,这是我最后一个问题的代码: getUserRole() { const headers = new Headers(); headers.append('Authorization', `Bearer ${this.getToken()}`); console.log(this.getToken()); const options = new RequestOptions({ headers: headers}); return this.http.get(`${this.
getUserRole() {
const headers = new Headers();
headers.append('Authorization', `Bearer ${this.getToken()}`);
console.log(this.getToken());
const options = new RequestOptions({ headers: headers});
return this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`,options).pipe(map(res => res.json()))
.subscribe(data => {
this.userRole = JSON.stringify(data);
if (this.userRole === 'parent') {
this.logout();
} else if (this.userRole === 'school') {
this.isUser = true;
this.isAdmin = false;
this.cookieService.set('isSchool', '1');
} else if (this.userRole === 'admin') {
this.isUser = false;
this.isAdmin = true;
this.cookieService.set('isAdmin', '1');
}
});
}
但是,当我在调用此函数后尝试访问userRole
时,我得到了userRole未定义的
,可能是因为它在命中之前返回了
因此,我将其作为未定义的:
var result = this.getUserRole(token);
console.log('Call from login ' + result);
因此,我将方法改为如下:
getUserRole(roletoken: string) {
const headers = new Headers();
headers.append('Authorization', `Bearer ${roletoken}`);
const options = new RequestOptions({ headers: headers});
var result = this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`, options).pipe(map(res => res.json()));
console.log(result);
return result;
}
["school"]
但在这里,我得到的是结果
作为[object object]
我想知道我应该采用哪种方法来立即获得由任一方法分配的userRole
在第二种方法中,我无法将[object object]
转换为值
我从API收到的数据如下:
getUserRole(roletoken: string) {
const headers = new Headers();
headers.append('Authorization', `Bearer ${roletoken}`);
const options = new RequestOptions({ headers: headers});
var result = this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`, options).pipe(map(res => res.json()));
console.log(result);
return result;
}
["school"]
这就是号召:
this.getUserRole(token);
console.log('Call from login ' + this.userRole);
这在getUserRole
函数中:
var result = this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`, options).subscribe(data => {
this.userRole = JSON.stringify(data);
console.log(data);
});
这是我得到的控制台的顺序:
来自登录名的调用未定义
答复{正文:“[“学校]”,状态:200,确定:正确,状态文本:
“确定”,标题:标题,…}
因此,即使使用subscribe尝试代码,userRole
的分配也会通过登录调用得到后者。我会做的是,
如果您的RESTAPI向您发送json
userRole: UserRole // here you set the type of userRole as UserRole that you defined earlier or in an other file
getUserRole() {
const headers = new Headers();
headers.append('Authorization', `Bearer ${this.getToken()}`);
console.log(this.getToken());
const options = new RequestOptions({ headers: headers});
return this.http.get<UserRole>(`${this.baseURL + this.loginApiRoot}/GetUserRoles`,options)
.subscribe(data => {
this.userRole = data;
if (this.userRole === 'parent') {
this.logout();
} else if (this.userRole === 'school') {
this.isUser = true;
this.isAdmin = false;
this.cookieService.set('isSchool', '1');
} else if (this.userRole === 'admin') {
this.isUser = false;
this.isAdmin = true;
this.cookieService.set('isAdmin', '1');
}
});
}
或
}
我不确定最后2个,因为我过去常常从我的API获得json响应,但这是我第一次检查并查看得到的结果
不要忘记,您可以使用浏览器调试器在每个步骤进行调试并查看变量的值,就像您在chrome中使用的一样,只需在函数的开头设置断点并遵循它;) 我会做的是,
如果您的RESTAPI向您发送json
userRole: UserRole // here you set the type of userRole as UserRole that you defined earlier or in an other file
getUserRole() {
const headers = new Headers();
headers.append('Authorization', `Bearer ${this.getToken()}`);
console.log(this.getToken());
const options = new RequestOptions({ headers: headers});
return this.http.get<UserRole>(`${this.baseURL + this.loginApiRoot}/GetUserRoles`,options)
.subscribe(data => {
this.userRole = data;
if (this.userRole === 'parent') {
this.logout();
} else if (this.userRole === 'school') {
this.isUser = true;
this.isAdmin = false;
this.cookieService.set('isSchool', '1');
} else if (this.userRole === 'admin') {
this.isUser = false;
this.isAdmin = true;
this.cookieService.set('isAdmin', '1');
}
});
}
或
}
我不确定最后2个,因为我过去常常从我的API获得json响应,但这是我第一次检查并查看得到的结果
不要忘记,您可以使用浏览器调试器在每个步骤进行调试并查看变量的值,就像您在chrome中使用的一样,只需在函数的开头设置断点并遵循它;) getUserRole
返回一个可观察的
。如您所见,您需要订阅它来进行HTTP调用并接收数据。由于您使用的是传统的Http
类,而不是HttpClient
,因此需要将响应转换为实际的JSON数据。下面的代码应该有效:
getUserRole(roletoken:string){
常量头=新头();
append('Authorization','Bearer${roletoken}`);
const options=newrequestoptions({headers:headers});
返回this.http.get(`${this.baseURL+this.loginApiRoot}/GetUserRoles`,options).pipe(map(response=>response.json());
}
理解可观察对象
和HTTP请求的异步性质很重要this.userRole
仅在请求完成后设置。因此,如果您想使用this.userRole
执行某些操作,并想确定它是否有值,则应在subscribe函数中使用它:
var result = this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`, options).subscribe(data => {
this.userRole = JSON.stringify(data);
console.log(data);
});
this.getUserRole(令牌).subscribe(userRole=>{
this.userRole=userRole;
console.log('callfromslogin'+this.userRole);
});
getUserRole
返回一个可观察的
。如您所见,您需要订阅它来进行HTTP调用并接收数据。由于您使用的是传统的Http
类,而不是HttpClient
,因此需要将响应转换为实际的JSON数据。下面的代码应该有效:
getUserRole(roletoken:string){
常量头=新头();
append('Authorization','Bearer${roletoken}`);
const options=newrequestoptions({headers:headers});
返回this.http.get(`${this.baseURL+this.loginApiRoot}/GetUserRoles`,options).pipe(map(response=>response.json());
}
理解可观察对象
和HTTP请求的异步性质很重要this.userRole
仅在请求完成后设置。因此,如果您想使用this.userRole
执行某些操作,并想确定它是否有值,则应在subscribe函数中使用它:
var result = this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`, options).subscribe(data => {
this.userRole = JSON.stringify(data);
console.log(data);
});
this.getUserRole(令牌).subscribe(userRole=>{
this.userRole=userRole;
console.log('callfromslogin'+this.userRole);
});
是的,我知道我从API中得到的结果是对象,而不是json,通过获取值实现了同样的效果。但问题不是这个。关键是我将在调用方法中使用变量后立即分配变量。Pl阅读我对Sajeetharanoh的评论,你是说问题是这个。userRole是在你想使用它之后定义的?如果是这样的话,您如何在其他ts文件或html文件中调用此函数?如果您使用Http,您是否检查您是否应该映射而不是订阅,在您的函数定义中是的,我知道我从API获得的结果是object,而不是json,并通过获取值实现了相同的结果。但问题不是这个。关键是我将在调用方法中使用变量后立即分配变量。Pl阅读我对Sajeetharanoh的评论,你是说问题是这个。userRole是在你想使用它之后定义的?如果是这样,您如何在其他ts文件或html文件中调用此函数?如果您使用Http,您是否检查了是否应该映射而不是订阅,在您的函数定义中,您使用的是Http
还是HttpClient
?我使用的是Http您使用的是Http
还是HttpClient
?我使用的是Httpit,它说的是undefined什么意思是undefined
?您希望如何使用getUserRole
的值?请注意,在传递给subscribe
的函数之外使用this.userRole
可能会导致未定义。这是由于HTTP调用的异步性质造成的。正确的答案实际上取决于你的想法