Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用Firebase将子集合查询到文档中_Angular_Typescript_Firebase_Google Cloud Firestore - Fatal编程技术网

Angular 使用Firebase将子集合查询到文档中

Angular 使用Firebase将子集合查询到文档中,angular,typescript,firebase,google-cloud-firestore,Angular,Typescript,Firebase,Google Cloud Firestore,我设置了一个angular 8 typescript接口,其中包含另一个接口的数组作为变量。如下所示: export interface User { name: string; id: string; history: Job[]; //this is what is not getting generated } export interface JobJson { id: string; price: number, notes: str

我设置了一个angular 8 typescript接口,其中包含另一个接口的数组作为变量。如下所示:

export interface User {
    name: string;
    id: string;
    history: Job[];  //this is what is not getting generated
}
export interface JobJson {
    id: string;
    price: number,
    notes: string
}
作业界面如下所示:

export interface User {
    name: string;
    id: string;
    history: Job[];  //this is what is not getting generated
}
export interface JobJson {
    id: string;
    price: number,
    notes: string
}
我使用Firebase的Cloud Firestore作为我的数据库,数据库结构如下所示:

用户//集合

|-->user1234//文档

|-->作业//子集合

|-->job1234//子文档

当我从firebase查询用户时:

this.firestore.collection<UserJson>('users').doc<UserJson>(id).valueChanges().subscribe((u) => {
    // do whatever with u
  });
this.firestore.collection('users').doc(id).valueChanges().subscribe((u)=>{
//对你做任何事
});

正如所料,它并没有填写历史记录:乔布斯[]。但是,在我的程序中,我需要将user.history数组设置为firebase中user下的子集合。这是怎么做到的?谢谢你的帮助

据我所知,不可能只打一次电话到firestore就得到这个信息。最接近的是两个调用解决方法(如果您知道哪个集合),或者三个调用解决方法(如果您不知道集合名称)

还有文档,documentReference的任何方法都无法获取文档快照和集合

在我的例子中,我将添加这两种解决方法,代码只是在控制台中打印内容

不知道子集合名称:它获取所有子集合及其元素:

db.collection('users').doc(str[6]).get().then(documentSnapshot => {
  console.log(documentSnapshot.data());
});

db.collection(COLLECTION_ID).doc(DOCUMENT_ID).listCollections().then(collections => {
  for (let collection of collections) {
    console.log(`Found subcollection with id: ${collection.id}`);
    collection.get().then((snapshot) => {
      snapshot.forEach((doc) => {
        console.log(doc.id, '=>', doc.data());
      });
    })
    .catch((err) => {
      console.log('Error getting documents', err);
    });
  }
});

了解要获取的子集合:

db.collection(COLLECTION_ID).doc(DOCUMENT_ID).get().then(documentSnapshot => {
  console.log(documentSnapshot.data());
});

db.collection(COLLECTION_ID).doc(DOCUMENT_ID).collection(SUBCOLLECTION_ID).get()
  .then((snapshot) => {
    snapshot.forEach((doc) => {
      console.log(doc.id, '=>', doc.data());
      console.log(doc.id, '=>', JSON.stringify(doc));
    });
  })

你想把子集合作业做好吗?@JoséSoní是的,带文档的子集合。我创建了一个只有两个调用的变通方法,一个用于文档用户,另一个用于JobJson