调用内部订阅的 Observable
Calling Observable having subscribe inside
我在 Observable 和 Subscriber 方面仍然存在一些问题,我面临的是,如果我从组件中调用 Observable 方法,我将无法获取数据。它是未定义的:
login(username: string, password: string): Observable<any> {
const body = {
'Mobile': username,
'Password': password,
};
return this.http.post(`${this.baseURL + this.loginApiRoot}/Login`, body)
.pipe(map((response: Response) => {
const jobj = JSON.parse(response.text());
const loginrequest = jobj['lr'];
const token = loginrequest['token'];
const is2auth = loginrequest['authtype'];
const schoolcode: string[] = jobj['schoolcode'];
// login successful if there's a jwt token in the response
if (token && token !== 'Invalid Login' ) {
if (token === 'Oops! seems you have not confirmed your email') {
return {result: false, message: token};
}
this.getUserRole(token).subscribe(userRole => {
this.userRole = userRole[0];
if (this.userRole === 'school' || this.userRole === 'admin') {
this.LoginSuccess(token, username, is2auth);
// return {result: true, message: token};
} else {
this.LoginFailed(token, username, is2auth);
// return {result: false, message: token};
}
});
} else {
// return false to indicate failed login
this.LoginFailed(token, username, is2auth);
return {result: false, message: token};
}
}));
}
调用方法:
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(
data => {
// tslint:disable-next-line:prefer-const
console.log(data);
let result = data.result;
并且此 console.log 正在返回未定义的数据。
来自另一个项目的这段代码运行良好:
login(username: string, password: string): Observable<any> {
var body = {
"Email":username,
"Password":password,
}
return this.http.post(`${this.loginApiRoot}/Login`, body)
.map((response: Response) => {
let jobj = JSON.parse(response.text());
let token = jobj["token"];
let is2auth=jobj["authtype"];
// login successful if there's a jwt token in the response
if (token && token!="Invalid Login" ) {
if (token =="Oops! seems you have not confirmed your email"){
return {result:false,message:token};
}
this.token = token;
this.isValidUser = true
this.userEmail = username;
this.setToken(username, token)
this.authType=is2auth;
if (is2auth==1)
{
this.isLogin=true;
this.is2Auth=true;
this.router.navigate(['/markets']);
return {result:true,message:token};
}
else if (is2auth>1)
{
this.isLogin = true;
this.is2Auth=false;
this.router.navigate(['/verify2auth']);
return {result:false,message:token};
}
else
{
}
}
else {
// return false to indicate failed login
return {result:false,message:token};
}
});
}
你应该在你的组件而不是你的服务中进行所有验证,只是 return 从你的服务中观察到的并在订阅中进行验证,
return this.http.post(`${this.baseURL + this.loginApiRoot}/Login`, body)
.pipe(map((response: Response) => {
return response;
}));
在组件中,
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(data => {
let result = data;
// do all your validations here
}
此外,您还在第一次调用中创建了另一个 http get 方法来获取 UserRole,您应该在从第一个调用中获取 reuslt 后将其作为一个函数分离出来。
您需要 return 根据您的要求在此行
if (this.userRole === 'school' || this.userRole === 'admin') {
this.LoginSuccess(token, username, is2auth);
// return {result: true, message: token};
} else {
this.LoginFailed(token, username, is2auth);
// return {result: false, message: token};
}
我在 Observable 和 Subscriber 方面仍然存在一些问题,我面临的是,如果我从组件中调用 Observable 方法,我将无法获取数据。它是未定义的:
login(username: string, password: string): Observable<any> {
const body = {
'Mobile': username,
'Password': password,
};
return this.http.post(`${this.baseURL + this.loginApiRoot}/Login`, body)
.pipe(map((response: Response) => {
const jobj = JSON.parse(response.text());
const loginrequest = jobj['lr'];
const token = loginrequest['token'];
const is2auth = loginrequest['authtype'];
const schoolcode: string[] = jobj['schoolcode'];
// login successful if there's a jwt token in the response
if (token && token !== 'Invalid Login' ) {
if (token === 'Oops! seems you have not confirmed your email') {
return {result: false, message: token};
}
this.getUserRole(token).subscribe(userRole => {
this.userRole = userRole[0];
if (this.userRole === 'school' || this.userRole === 'admin') {
this.LoginSuccess(token, username, is2auth);
// return {result: true, message: token};
} else {
this.LoginFailed(token, username, is2auth);
// return {result: false, message: token};
}
});
} else {
// return false to indicate failed login
this.LoginFailed(token, username, is2auth);
return {result: false, message: token};
}
}));
}
调用方法:
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(
data => {
// tslint:disable-next-line:prefer-const
console.log(data);
let result = data.result;
并且此 console.log 正在返回未定义的数据。
来自另一个项目的这段代码运行良好:
login(username: string, password: string): Observable<any> {
var body = {
"Email":username,
"Password":password,
}
return this.http.post(`${this.loginApiRoot}/Login`, body)
.map((response: Response) => {
let jobj = JSON.parse(response.text());
let token = jobj["token"];
let is2auth=jobj["authtype"];
// login successful if there's a jwt token in the response
if (token && token!="Invalid Login" ) {
if (token =="Oops! seems you have not confirmed your email"){
return {result:false,message:token};
}
this.token = token;
this.isValidUser = true
this.userEmail = username;
this.setToken(username, token)
this.authType=is2auth;
if (is2auth==1)
{
this.isLogin=true;
this.is2Auth=true;
this.router.navigate(['/markets']);
return {result:true,message:token};
}
else if (is2auth>1)
{
this.isLogin = true;
this.is2Auth=false;
this.router.navigate(['/verify2auth']);
return {result:false,message:token};
}
else
{
}
}
else {
// return false to indicate failed login
return {result:false,message:token};
}
});
}
你应该在你的组件而不是你的服务中进行所有验证,只是 return 从你的服务中观察到的并在订阅中进行验证,
return this.http.post(`${this.baseURL + this.loginApiRoot}/Login`, body)
.pipe(map((response: Response) => {
return response;
}));
在组件中,
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(data => {
let result = data;
// do all your validations here
}
此外,您还在第一次调用中创建了另一个 http get 方法来获取 UserRole,您应该在从第一个调用中获取 reuslt 后将其作为一个函数分离出来。
您需要 return 根据您的要求在此行
if (this.userRole === 'school' || this.userRole === 'admin') {
this.LoginSuccess(token, username, is2auth);
// return {result: true, message: token};
} else {
this.LoginFailed(token, username, is2auth);
// return {result: false, message: token};
}