Angularfire2,Firestore 文档检索问题(使用 .get,可能与 ngOnChanges 有关)

Angularfire2, Firestore Document Retrieval Issue (using .get, possibly related to ngOnChanges)

我的应用程序结构有 1 个服务和 2 个组件:

服务:持有所有 Firestore 连接 ViewComponent:从服务中获取一个列表并显示出来 ItemManagementComponent:显示表单并允许用户添加新条目。

问题 我在 ViewComponent 中有一个按钮(理论上)将 documentID 发送到 ManagementComponent,然后在表单中显示该文档数据,允许用户更新。现在,当我单击该按钮时,我可以在我的应用程序中一直跟踪 doumentID 和服务 return,但我实际上并没有得到文档 returned。我可以 运行 使用硬连线 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 ,并且该服务具有对 return.

的格式正确的文档响应

如果我直接从我的控制台日志中复制一个文档 ID,并将其硬连接到 ItemManagementComponent 上的 updateInventory 函数,并从 NgOnChanges 调用硬连接函数,它就会工作。只是不知何故,当我将此 ID 作为变量传递时,它会短路。

我有一个相同的 updateInventory 函数,但它的实现略有不同,它采用了一个变量,所以我真的很困惑。非常感谢任何帮助!

问题 作为一个新手,我使用了很多代码示例来让事情正常运行,然后尝试弄清楚为什么它有效等等。在这个例子中,我发现的代码片段是使用 JSON.stringify 来设置 documentID 值.没有意识到后果,我把它留在了里面。

let cur = JSON.stringify(chng.currentValue);

我缺少的是控制台中的一个非常微妙的(对我来说)线索。

**Console Return** service "X7hFhwDLzFIicl5uUCEX"

我以为引号只是控制台中的标准符号。直到一时兴起,我在视图组件的原始按钮上添加了一个 console.log,并注意到我的 return 从那里没有引号:

service X7hFhwDLzFIicl5uUCEX

解决这个问题的唯一方法是将上面的字符串化行更改为:

let cur: string = chng.currentValue;

否则其余代码似乎正在执行我想要的操作。感谢任何看过的人!