ID动态文件Firestore [英] ID dynamic document firestore

查看:46
本文介绍了ID动态文件Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使Firestore QEoevSjHlswgk44nVTsr 中的此文档的ID动态化,即根据该集合中的ID进行更改?我正在使用angularfire2:^ 5.0.0-rc.11

How can I make the ID of this document in firestore QEoevSjHlswgk44nVTsr dynamic, that is, change according to the IDs in that collection? I'm using angularfire2: ^5.0.0-rc.11

这就是我所拥有的:

firebase.service.ts

proyectos: Observable<Proyecto[]>;

getImagenDestacada() {
    this.proyectos = this.afs.collection('proyectos').doc('QEoevSjHlswgk44nVTsr').collection('archivos', ref => ref
    .orderBy('nombre','asc').limit(1)
    ).snapshotChanges().pipe(map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data() as Proyecto;
        const id = a.payload.doc.id; 
        return { id, ...data };
      });
    }));
    return this.proyectos
    }

======================

========================

我尝试了以下操作:

firebase.service.ts

getImagenDestacada(id:string) {
this.proyectos = this.afs.collection('proyectos').doc(id).collection('archivos', ref => ref
.orderBy('nombre','asc').limit(1)
).snapshotChanges().pipe(map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data() as Proyecto;
    const id = a.payload.doc.id; 
    return { id, ...data };
  });
}));
return this.proyectos
}

portafolio.component.ts

archivos: Observable<Proyecto[]>;

this.archivos = this.fs.getImagenDestacada(this.id);

但是出现以下错误:

错误:函数CollectionReference.doc()要求其第一个参数为字符串类型,但它是:未定义

Error: Function CollectionReference.doc() requires its first argument to be of type string, but it was: undefined

推荐答案

该错误是因为您致电

this.proyectos = this.afs.collection('proyectos').doc(id).collection ...

id 是未定义的,因为在调用方中未定义 this.id .

id is undefined, because this.id was undefined in the caller.

如果需要,请根据 Firebase文档动态生成的ID,则只需将参数保留为空白即可.将该行更改为:

According to the Firebase documentation, if you want a dynamically generated id then you can simply leave the parameter blank, ie. change that line to be :

this.proyectos = this.afs.collection('proyectos').doc().collection ...

然后Firebase会为您生成一个ID.

Then Firebase will generate an ID for you.

如果另一方面,您想要记录具有特定的ID,那么您需要一些代码来为您获取该ID,并将其传递给您的 getImagenDestacada 函数而不是 this.id .

If, on the other hand, there is a specific ID you want the record to have, then you need to have some code to get that ID for you and pass that through to your getImagenDestacada function instead of this.id.

这篇关于ID动态文件Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆