您好,我正在开发一个角度/打字稿应用程序,我已经阅读了有关promise
的信息,但我不清楚您的帮助。
有函数getUserById()
返回用户信息,getAttributeByUserId()
,所以我需要从两个函数中填写表单,但getAttribute
上的变量undefinied
,这是我的代码。
角度/打字稿
getUserById(userId, modalContent) {
console.log('get user by id ' + userId);
const config = AppConfiguration.CONFIG_GET_USERS_BY_ID;
config.user_id = userId;
const date = new Date();
this._httpService.CountUsers().post(config).subscribe((data: any) => {
console.log('resultado al obtener usuario editado ' + data[0].user_id);
this.userForm.patchValue({'firstName': data[0].firstname});
this.userForm.patchValue({'secondName': data[0].secondname});
this.userForm.patchValue({'lastName': data[0].lastname});
this.userForm.patchValue({'mothersLastName': data[0].motherslastname});
this.userForm.patchValue({'birthDay': {
date: {
year: data[0].birthday.substring(0, 4),
month: data[0].birthday.substring(5, 7),
day: data[0].birthday.substring(8, 10)}
}});
this.userForm.patchValue({'roleId': data[0].role_id});
this.userForm.patchValue({'email': data[0].email});
this.userForm.patchValue({'userId': data[0].user_id});
this.open(modalContent);
// this.open(modalContent);
});
}
getAttributeByUserId(userId: number) {
const config = AppConfiguration.CONFIG_GET_ATTRIBUTE_BY_ID;
config.user_id = userId;
this._httpService.CountUsers().post(config).subscribe((data: any) => {
this.attributes = data;
});
}
我需要在getUserById
内部调用getAttributeByUserId
并获取数据。
看到你的答案后,你问的是什么就更清楚了。您需要返回一个可以从 getAttributeByUserId
订阅的可观察量。如果您仍想将返回的值推送到attributes
那么您可以使用 tap
在该方法中执行此操作,这允许您在执行订阅回调之前执行代码而不更改返回的结果,或者您可以让调用方在subscribe
回调中处理这个问题。我选择了前者。
在学习如何使用打字稿/角度进行开发时,另一个关键建议是:学习使用强类型。这可以节省以后更难调试或捕获的运行时错误。我推断下面有一些名为Attribute
的接口或类,如果你还没有,我建议使用接口(从http调用返回的数据结构的常见做法(。
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
getUserById(userId, modalContent) {
console.log('get user by id ' + userId);
const config = AppConfiguration.CONFIG_GET_USERS_BY_ID;
config.user_id = userId;
const date = new Date();
this._httpService.CountUsers().post(config).subscribe((data: any) => {
console.log('resultado al obtener usuario editado ' + data[0].user_id);
this.userForm.patchValue({'firstName': data[0].firstname});
this.userForm.patchValue({'secondName': data[0].secondname});
this.userForm.patchValue({'lastName': data[0].lastname});
this.userForm.patchValue({'mothersLastName': data[0].motherslastname});
this.userForm.patchValue({'birthDay': {
date: {
year: data[0].birthday.substring(0, 4),
month: data[0].birthday.substring(5, 7),
day: data[0].birthday.substring(8, 10)}
}});
this.userForm.patchValue({'roleId': data[0].role_id});
this.userForm.patchValue({'email': data[0].email});
this.userForm.patchValue({'userId': data[0].user_id});
this.getAttributeByUserId(userId).subscribe((attributes) => {
this.open(modalContent);
});
});
}
getAttributeByUserId(userId: number) : Observable<Attribute[]> {
const config = AppConfiguration.CONFIG_GET_ATTRIBUTE_BY_ID;
config.user_id = userId;
return this._httpService.CountUsers().post<Attribute[]>(config).pipe(tap((data) => {
this.attributes = data;
}));
}
我更改了函数
getUserById(userId, modalContent) {
console.log('get user by id ' + userId);
const config = AppConfiguration.CONFIG_GET_USERS_BY_ID;
config.user_id = userId;
const date = new Date();
this._httpService.CountUsers().post(config).subscribe((data: any) => {
console.log('resultado al obtener usuario editado ' + data[0].user_id);
this.userForm.patchValue({'firstName': data[0].firstname});
this.userForm.patchValue({'secondName': data[0].secondname});
this.userForm.patchValue({'lastName': data[0].lastname});
this.userForm.patchValue({'mothersLastName': data[0].motherslastname});
this.userForm.patchValue({'birthDay': {
date: {
year: data[0].birthday.substring(0, 4),
month: data[0].birthday.substring(5, 7),
day: data[0].birthday.substring(8, 10)}
}});
this.userForm.patchValue({'roleId': data[0].role_id});
this.userForm.patchValue({'email': data[0].email});
this.userForm.patchValue({'userId': data[0].user_id});
this.getAttributeByUserId(data[0].user_id).then((response) => {
console.log('respueta del ws attribuete' + JSON.stringify(response));
this.open(modalContent);
});
// this.open(modalContent);
});
}
getAttributeByUserId(userId: number) {
const config = AppConfiguration.CONFIG_GET_ATTRIBUTE_BY_ID;
config.user_id = userId;
this._httpService.CountUsers().post(config).subscribe((data: any) => {
this.attributes = data;
});
return Promise.resolve(this.attributes);
}