Enrique GF
Enrique GF

Reputation: 1295

Updating a field inside a firebase firestore doc without hand write the field key to update Angular

How could i update a doc inside my firestore without precisely write in the code the field i want to update , cause already is reached through the form: .Lets say the HTML tag in Angular already brings both the key and the import(that one to be updated):

HTML Tags
       <form [formGroup]="importNgForm" (submit)="addUniqueImport()">
            <div class="modal-body">
              <div class="form-group">
                <label for="key"></label>
                <input disabled type="text" value='{{incomeSelected}}' name="key" class="form-control" formControlName="key" />
               </div>

              <div class="form-group">
                <label for="import">Add amount</label>
                <input type="number" name="import" class="form-control" formControlName="import" />
               </div>
             </div>

             <div class="modal-footer">
              <button type="submit" class="btn btn-primary">Add</button>
             </div>
......more code

Then on my component for that html :

some imports...

export class UserSheetBalanceComponent implements OnInit {

importNgForm: FormGroup;

 constructor(
    private service: Service,
    private amountBuilder:FormBuilder,
  ) {
    this.importNgForm = this.amountBuilder.group({
      key:new FormControl(),
      import:new FormControl(),
    });
  }

  addUniqueImport() {
    this.service.addingImport(this.importNgForm.value as addImport)
  }

and then finally on my service component i just try to pass the parameters the form brings :

 addingImport(dataToPass: addImport) {

    const path = this.docCreator
      .collection('users')
      .doc(this.userdata.uid)
      .collection('Incomings')
      .doc(this.userdata.uid);=====>Path reaching the doc to update

Until declaring the path to access that document where the field is.But then when try to refer the name of the field i want to update through the form (dataToPass.key) ,and the import for this field im doing reference to (dataToPass.import) the error appears.

   path.update({dataToPass.key:dataToPass.import}) ====>doesn't work
   }

The problem is in the key, let say i instead of accessing my form(dataToPass) i write directly the name of the field to update(some name),i does work

   path.update({some name:dataToPass.import}) ======>does work
   }

so im wondering how could i access that field key without precisely write it , but dynamically, in order to update my import once the field on query matchs Thanks in advance!!!!

Upvotes: 0

Views: 338

Answers (1)

Radik
Radik

Reputation: 2795

if you have reference to object in firebase

const path = this.docCreator
  .collection('users')
  .doc(this.userdata.uid)
  .collection('Incomings')
  .doc(this.userdata.uid);

you can crete empty object and use key name from dataToPass.key to set property

let foo: any = {};
foo[`${dataToPass.key}`] = dataToPass.import;
path.update(foo);

Upvotes: 2

Related Questions