无需手动更新Firebase Firestore文档中的字段即可编写字段密钥以更新Angular [英] Updating a field inside a firebase firestore doc without hand write the field key to update Angular
问题描述
我如何在Firestore中更新文档,而没有在代码中准确地写我要更新的字段,原因是已经通过以下形式到达了该字段:.让我们说Angular中的HTML标签已经带来了密钥和导入(要更新的密钥):
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
然后在我的HTML组件上:
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
直到声明访问该字段所在位置的文档的路径.但是当尝试引用该字段的名称时,我想通过表单(dataToPass.key)进行更新,并且对该字段的导入正在引用(dataToPass.import)出现错误.
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
}
问题出在键上,比方说我而不是访问表单(dataToPass),而是直接写要更新的字段名称(某些名称),我确实可以工作
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!!!!
推荐答案
如果您引用了Firebase中的对象
if you have reference to object in firebase
const path = this.docCreator
.collection('users')
.doc(this.userdata.uid)
.collection('Incomings')
.doc(this.userdata.uid);
您可以创建空对象,并使用 dataToPass.key
中的键名来设置属性
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);
这篇关于无需手动更新Firebase Firestore文档中的字段即可编写字段密钥以更新Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!