Firestore:检索单个文档

埃里克

我想从Firestore检索单个文档。因此,我并不是说集合中的文档列表(我知道该怎么做)。假设我知道文档的键值对之一,并且想在Angular 4中查找和检索该文档。

interface Occupations {
	duration: number;
	id_entity: number;
	id_job: number;
	id_user: number;
	salary: number;
	start_time: number;
}

occupationDoc:AngularFirestoreDocument<Occupations>;
occupation: Observable<Occupations>;
<h1>A specific post:</h1>

  <h3>{{ (occupation | async)?.salary }}</h3>
  <p>{{ (occupation | async)?.id_user }}</p> 
  <p>{{ (occupation | async)?.duration }}</p> 
  <p>{{ (user | async)?.lastName }}</p> 

蒂芬·梅洛尼(Tiffan Meloney)

使用平面图返回单个文档数据:

首先导入.flatMap()属性。

import { AngularFirestore } from 'angularfire2/firestore';
import 'rxjs/add/operator/mergeMap';

然后查询您的集合并将其限制为1个文档:

this.afs.collection('collection', ref => ref.where('value', '==', true) 
.limit(1)).valueChanges().flatMap(result => result);

然后,您可以订阅它,它将返回平化的json而不是数组。

编辑:

您可以这样做,直接在html中使用它:

this.userInfo = this.afs.collection('collection', ref => ref.where('value', 
'==', true).limit(1)).valueChanges().flatMap(result => result);

在html代码中:

<p>{{ (userInfo | async)?.duration }}</p>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章