繁体   English   中英

存储来自 service.ts 的数据以在同一个 service.ts 中使用

[英]storing data from service.ts to use in the same service.ts

我有 firestore.service.ts,在一个方法中,我在 firebase 中创建和更新了一个集合......离开该方法后,我更改了集合的“id”,但我需要这个“id”不会丢失,因为我需要它到下一个方法。 我怎样才能保留这个“id”值?

  public createFormulario ( data ) {
        return this.firestore.collection('formularios').add(data).then
        (ref => {
          data.id = ref.id; <==keep this data.id
         this.firestore.collection('formularios').doc( ref.id ).update(data);
        });

  public createPregunta1 ( data: any ) {

    this.firestore.collection('formularios').doc( 'i need the above data.id here' ).update(data);
}

所以,我需要从 createFormulario 中捕获 data.id... 并在名为 createPregunta1 的其他方法中使用它。

显然,当应用程序运行并从第一页传递到第二页时。 该 data.id 为空

您可以在您的服务/类中声明一个变量,例如id: number; 然后可以在您班级的任何地方引用它。

所以在你的createFormulario方法中你可以说this.id = ref.id; 这将全局存储 id(在服务内),您可以在createPreguntal方法this.id其引用为this.id

export class FirestoreService {
  //declare our class variables
  id: number;

  public createFormulario ( data ) {
      return this.firestore.collection('formularios').add(data).then
          (ref => {
            this.id = ref.id; //store the id in our class variable
            this.firestore.collection('formularios').doc( ref.id ).update(data);
          });
  }

  public createPregunta1 ( data: any ) {
      this.firestore.collection('formularios').doc(this.id).update(data);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM