【发布时间】:2018-12-02 23:40:49
【问题描述】:
我的应用程序结构有 1 个服务和 2 个组件:
Service:持有所有的 Firestore 连接 ViewComponent:从服务中获取列表并显示 ItemManagementComponent:显示一个表单并允许用户添加一个新条目。
问题 我在 ViewComponent 中有一个按钮(理论上),它向 ManagementComponent 发送一个 documentID,然后在表单中显示该文档数据,从而允许用户更新。现在,当我单击按钮时,我可以跟踪 doumentID,并且服务一直通过我的应用程序返回,但我实际上并没有得到返回的文档。我可以使用硬连线的 documentID 运行完全相同的代码,而且效果很好,所以显然我遗漏了一些东西。
服务
getUpdateInventoryItem(updateId: string) {
console.log('service', updateId)
this.updateId = updateId
console.log('service2', this.inventoryCollectionRef.doc(updateId).ref.get())
return this.inventoryCollectionRef.doc(updateId).ref.get()
}
查看 HTML
<td><button type="button" class="btn btn-default" (click)="updateInventory(item.id)">Upd</button></td>
<app-item-management [updateID]="ChildUpdateId"></app-item-management>
查看组件
@Output() inventoryUpdate = new EventEmitter();
public ChildUpdateId: string;
updateInventory(updateId: string) {
this.ChildUpdateId = updateId;
}
物品管理组件
@Input() updateID;
ngOnChanges(incomingID: SimpleChanges) {
if (!incomingID['updateID'].isFirstChange()) {
for (let ID in incomingID) {
let chng = incomingID[ID];
let cur = JSON.stringify(chng.currentValue);
console.log('current', cur)
this.updateInventory(cur)
}
}
}
updateInventory(incomingID) {
console.log('updateFunction',incomingID)
this.inventoryService.getUpdateInventoryItem(incomingID)
.then((doc) => {
if (doc.exists) {
this.editItem = doc.data();
this.inventoryForm.patchValue(this.editItem);
this.update = true;
} else {
console.log("No such document!");
}
}).catch(function (error) {
console.log("Error getting document:", error);
});
};
所以,如果我单击视图 HTML 上的更新按钮,这就是我的控制台向我显示的内容:console log screenshot 从我的角度来看,我看到了传递给服务的 ID,并且服务的格式正确记录要返回的响应。
如果我直接从控制台日志复制文档 ID 并将其硬连接到 ItemManagementComponent 上的 updateInventory 函数,然后从 NgOnChanges 调用硬连线函数,它就可以工作。只是当我将此 ID 作为变量传递时,它会以某种方式短路。
我有同样的 updateInventory 函数,它使用一个稍微不同的实现,它接受一个变量,所以我真的很难过。非常感谢任何帮助!
【问题讨论】:
标签: angular firebase google-cloud-firestore angularfire2 ngonchanges