我试图在Firebase中更新子值。
这是HTML:
<ion-item-group>
<ion-item-divider color="light">We Need</ion-item-divider>
<ion-item *ngFor="let list of lists" (click)="unlist()">{{list?.foodname}}</ion-item>
</ion-item-group>
在TS中声明了数组:
export class HomePage implements OnInit {
foods;
lists;
startAt = new Subject()
endAt = new Subject()
TS中的功能是:
unlist() {
firebase.database().ref('foods').child()
.update({ state: "unlisted" });
}
这是我数据库的结构:
{
"foods" : {
"foodID1" : {
"category" : "Produce",
"foodname" : "Apples",
"state" : "listed"
},
"foodID2" : {
"category" : "Dairy",
"foodname" : "Cheese",
"state" : "listed"
},
"foodID3" : {
"foodname" : "Eggs",
"state" : "unlisted"
},
"foodID4" : {
"category" : "Dairy",
"foodname" : "Milk",
"state" : "unlisted"
},
"foodID5" : {
"category" : "Produce",
"foodname" : "Onions",
"state" : "unlisted"
}
}
}
现在,触发此功能会添加&#34; state&#34;到&#34;食物&#34;而不是改变已存在的状态。
如何在不手动输入&#34; FoodID1&#34;,&#34; FoodID2&#34;等的情况下获取孩子的价值?
答案 0 :(得分:0)
我能够通过将onClick事件更改为(click)="unlist(list)"
并将函数更改为:
unlist(list) {
firebase.database().ref('foods').child(list.$key)
.update({ state: "unlisted" });}