Firestore:检索单个文档 [英] Firestore : Retrieve a single document

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

问题描述

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

I want to retrieve a single document from Firestore. So I do not mean the list of documents in the collection (I know how to do that). Let's say I know one of the key-value pair of the document and I would like to find and retrieve the document into 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> 

推荐答案

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

Use flatmap to return the single document data:

首先导入.flatMap()属性.

First import the .flatMap() property.

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

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

Then query your collection and limit it to 1 document:

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

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

Then you can subscribe to that and it will return the flatened json instead of an array.

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

And you can just do like this to use it in your html directly:

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

在html代码中:

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

这篇关于Firestore:检索单个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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