【问题标题】:Display collection document content in the input Firestore Angular在输入 Firestore Angular 中显示集合文档内容
【发布时间】:2018-11-26 16:19:05
【问题描述】:

我正在尝试在输入字段中显示来自 Firestore 的数据,但最终出现错误

intem.services.ts

getItem(itemId) {
  return this.afs.doc('items' + itemId);
}

item.component.ts

item;
constructor(
  private router: Router,
  private afs: AngularFirestore,
  private route: ActivatedRoute,
  private itemService: ItemService) {

  const id = this.route.snapshot.paramMap.get('id');
  if (id) {this.itemService.getItem(id).snapshotChanges().pipe(take(1), map(p => this.product = p));
}

item.component.html

<input #title="ngModel" [(ngModel)]="item.title" name="title" type="text" id="title" class="form-control">

【问题讨论】:

  • 请提供您遇到的错误。一个最小的例子会有帮助
  • ERROR CONTEXT Object { view: {…}, nodeIndex: 13, nodeDef: {…}, elDef: {…}, elView: {…} } IntemFormComponent.html:6:12
  • 您尝试过此处说明的修复方法吗? stackoverflow.com/questions/44879095/…
  • ERROR TypeError: _co.item is undefined
  • 是,但没用

标签: angular firebase google-cloud-firestore


【解决方案1】:

尝试获取ngAfterViewInit() or ngAfterContentInit() 中的数据。另外我注意到你的数据结构是什么样的,但如果你有一个 id 并且想要一个文档,它是return this.afs.doc(`items/${itemId}`);

【讨论】:

  • 不工作。我改变这样的代码: getItem(itemId) { return this.afs.doc(items/${itemId});并在我的 item.component.ts 我从 item 改变;到项目 = {};现在我没有收到任何错误,但我没有从 Firestore 获得返回数据。数据结构就像项目,然后自动生成文档 ID,然后我得到一些信息,如标题:,一些描述:等。在这个例子中,我想显示标题
  • 在我的 my-item.component.html 我有一个链接 Edit 当我点击编辑将我返回到带有 ID od 项目的 items.component,在 URL 中单击,但永远不会从输入字段中的 firestore 项目中获取数据
  • 您是否也尝试将getItem(id) 从构造函数中取出?试试这个firebase.firestore().collection("items").doc(ItemId).onSnapshot(doc =&gt;{ console.log(doc.data()) })。在组件初始化后使用临时按钮单击以检查控制台中是否有内容。您还可以使用 firestore 中的实际 id 来检查函数是否返回任何内容。
猜你喜欢
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2021-02-20
相关资源
最近更新 更多