等待诺言的回应以填写表格

时间:2019-04-22 16:28:49

标签: angular typescript asynchronous observable

您好,我正在研究角度/打字稿应用程序,已经阅读了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;
  });
}

我需要在getAttributeByUserId内致电getUserById并获取数据。

2 个答案:

答案 0 :(得分:0)

我更改功能

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);

}

答案 1 :(得分:0)

看到您的答案后,您所要问的内容就更清楚了。您需要返回可从getAttributeByUserId订阅的观察对象。如果您仍然想将返回值推入attributes,则可以使用tap在该方法中执行该操作,该方法允许您在执行订阅回调之前执行代码而无需更改返回结果。或者,您可以在subscribe回叫中让呼叫者解决。我选择了前者。

在学习如何使用打字稿/ angular进行开发时的另一项重要建议:学习使用强类型。这样可以为您节省以后难以调试或捕获的运行时错误。我推断下面有一些名为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;
  }));
}