Typescript 如何使用map操作符将对象的新字段添加到返回到可观察对象的数组中?

Typescript 如何使用map操作符将对象的新字段添加到返回到可观察对象的数组中?,typescript,rxjs,Typescript,Rxjs,我是RxJS的新手,正在处理一个角度问题,我有以下问题 在我的组件中,我正在使用此服务方法检索一个可观察对象,它正在从Firebase FireStore DB检索它: getAllPatients(): Observable<Patient[]> { return <Observable<Patient[]>> <unknown>this.firestore.collection('patients').snapshotCh

我是RxJS的新手,正在处理一个角度问题,我有以下问题

在我的组件中,我正在使用此服务方法检索一个可观察对象,它正在从Firebase FireStore DB检索它:

  getAllPatients(): Observable<Patient[]> { 
   
    return <Observable<Patient[]>> <unknown>this.firestore.collection('patients').snapshotChanges()
    .pipe(
      map(snaps => 
        snaps
          .map(snap => snap.payload.doc)
          .map(doc => ({
            UID: doc.id,
            id: doc.id,
            firstName: doc.get("firstName"),
            surname: doc.get("surname"),
            completeName: `${doc.get("firstName")} ${doc.get("surname")}`,
            birthDate: doc.get("birthDate"),
            placeOfBirth: doc.get("placeOfBirth"),
            socialSecurityCode: doc.get("socialSecurityCode"),
            personalEmail: doc.get("personalEmail"),
            personalPhone: doc.get("personalPhone"),
            occupation: doc.get("occupation"),
            contactReason: doc.get("contactReason")
          }))
      )
    );

  }
如您所见,它包含以下两个字段:

出生日期:任何人;:它是从FireStore检索的,是一个日期字段。 birthDateStr?:它不是从FireStore中检索的,我必须从上一个birthDate字段开始以字符串形式构建它。 因此,在我的组件代码中,最初我有这样的东西来检索我的数据,它工作得很好:

// Load the list of all the patients from the DB:
async loadPatientsList() {
    this.patientsList$ = await this.patientService.getAllPatients()
    console.log("patientsList$: ", this.patientsList$);
    //this.loading = false;
}
现在我的问题是,对于我的可观察对象的Patient[]数组中的每个Patient对象,我必须添加这个birthDateStr?将当前对象的生日字段值设置为字符串的额外字段

我认为我必须使用RxJS,我正试图这样做:

// Load the list of all the patients from the DB:
async loadPatientsList() {
    this.patientsList$ = await this.patientService.getAllPatients()
        .pipe(
          map(fields => {
            let temp = { ...fields, 
                         birthDateStr: fields.birthDate..... // HERE I DON'T KNOW WHAT TO DO !!!
                       }
            return temp;
          })
        );
    console.log("patientsList$: ", this.patientsList$);
    //this.loading = false;
}
所以基本上我尝试使用RxJS映射操作符来添加这个字段。基本上,我正在创建一个全新的临时对象,其中包含原始对象的所有字段

然后,我尝试从应该包含在原始字段中的生日开始创建新的birthDateStr,行为:

birthDateStr: fields.birthDate..... // HERE I DON'T KNOW WHAT TO DO !!!
问题是,现在IDE在birthDate属性的前一行上给了我以下错误:

类型“Patient[]”上不存在属性“birthDate”。ts2339

为什么??怎么了?我错过了什么?如何解决此问题?此外,如何完成代码以将此日期字段转换为表示日期的字符串?

看起来字段实际上是一个患者数组:Patient[]

因此,您需要映射该数组,以便为每个患者添加新的birthDateStr字段:

// Load the list of all the patients from the DB:
async loadPatientsList() {
  this.patientsList$ = await this.patientService.getAllPatients()
    .pipe(
      map(patients => {
        return patients.map(fields => {
          return {
            ...fields, 
            birthDateStr: fields.birthDate // <-- do whatever conversion you need here
          };
        })
      });
}
// Load the list of all the patients from the DB:
async loadPatientsList() {
  this.patientsList$ = await this.patientService.getAllPatients()
    .pipe(
      map(patients => {
        return patients.map(fields => {
          return {
            ...fields, 
            birthDateStr: fields.birthDate // <-- do whatever conversion you need here
          };
        })
      });
}
getAllPatients(): Observable<Patient[]> {   
  return <Observable<Patient[]>> <unknown>this.firestore.collection('patients').snapshotChanges()
    .pipe(
      map(snaps => 
        snaps
          .map(snap => snap.payload.doc)
          .map(doc => ({
            UID: doc.id,
            id: doc.id,
            firstName: doc.get("firstName"),
            surname: doc.get("surname"),
            completeName: `${doc.get("firstName")} ${doc.get("surname")}`,
            birthDate: doc.get("birthDate"),
            placeOfBirth: doc.get("placeOfBirth"),
            socialSecurityCode: doc.get("socialSecurityCode"),
            personalEmail: doc.get("personalEmail"),
            personalPhone: doc.get("personalPhone"),
            occupation: doc.get("occupation"),
            contactReason: doc.get("contactReason"),
            birthDateStr: "..." // <-- HERE
          }))
      )
    );
}