Firestore: Add Custom Object to db

前端 未结 8 2036
庸人自扰
庸人自扰 2020-12-15 05:31

Good Morning,

I tried adding a new created object from this class:

export class Sponsor implements ISponsor {

  title: string;    
  description?: s         


        
8条回答
  •  渐次进展
    2020-12-15 05:43

    It's really strange behavior from firebase. And that how I fixed it - by creating new Interface and adding convertation method to my class:

    export class Happening {
     constructor(
      public date: EventDate,
      public participants: Array,
      public title: string,
      public text: string,
      public uid?: string,
      public id?: string
     ){}
    
     public toDto = (): HappeningDto => {
      return {
        date: {
          year: this.date.year,
          month: this.date.month,
          day: this.date.day
        },
        participants: this.participants ? this.participants : [],
        title: this.title,
        text: this.text ? this.text : '',
        uid: this.uid,
        id: this.id ? this.id : null
      }
     }
    }
    
    export interface HappeningDto {
     date: {
      year: number,
      month: number,
      day: number
     },
     participants: Array,
     title: string,
     text: string,
     uid?: string,
     id?: string
    }
    

    Now, I can do

    add(event: Happening){
      event.uid = this.uid;
      this.$afs.collection('events').add(event.toDto())
        .then(
          (success) => console.log(success),
          (err) => console.warn(err)
        )
    }
    

提交回复
热议问题