Firestore:检索单个文档

时间:2017-11-08 15:51:28

标签: javascript angular rxjs google-cloud-firestore angularfire5

我想从Firestore检索单个文档。所以我不是指集合中的文档列表(我知道该怎么做)。假设我知道文档的一个键值对,我想找到并将文档检索到Angular 4中。

<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> 
 <form class="form-horizontal" name="paymentInformation"      id="paymentInformation" action="verifyOrder.cfm" method="post" role="form">
   <div class="form-group">
     <fieldset class="col-sm-12">

  <!-- Row 1 -->
  <div class="row">
    <div class="col-sm-6">
      <label for="booktype" class="col-6 col-form-label">
        Book Type *
      </label>
      <select class="custom-select col-4" id="booktype" name="booktype">
        <option selected value="">Select book type</option>
        <option value="val1">E-BOOK</option>
      </select>
    </div>

    <div class="col-sm-6">
      <label for="id2" class="col-6 col-form-label">
        Number
      </label>
      <input type="form-control" placeholder="Number" type="text" id="id2" name="id2" class="col-10">
    </div>

  </div>
  <!-- /Row 1 -->
  <!-- Row 2 -->
  <div class="row">
    <div class="col-sm-6">
      <label for="firstname" class="col-6 col-form-label">
        First Name *
      </label>
      <input type="form-control" type="text" id="firstname" name="firstname" class="col-12" placeholder="Name">
    </div>
    <div class="col-sm-6">
      <label for="id2" class="col-6 col-form-label">
        Book Name
      </label>
      <input type="form-control" placeholder="Book Name" type="text" id="id3" name="id3" class="col-10">
    </div>
  </div>
  <!-- /Row 2 -->
  <div class="row">
    <div class="col-sm-6">
      <label for="lastname" class="col-6 col-form-label">
        Last Name *
      </label>
      <input type="form-control" type="text" id="lastname" name="lastname" class="col-12" placeholder="Name">
    </div>
  </div>
  <!-- /Row 2 -->
  <label for="description" class="col-10 col-form-label">
    Country description
  </label>
  <textarea id="description" name="description" rows="4" class="form-control txtarea-rounded"></textarea>
  <div class="form-check">
    <label class="col-sm-6">Countries: </label>
    <br />
    <label class="form-check-label col-10">
      <input class="form-check-input col-10" type="checkbox" name="usa" value="Y" id="usa">USA
      <br />
      <input class="form-check-input col-10" type="checkbox" name="uk" value="Y" id="uk"> UK
    </label>
  </div>

</fieldset>
   </div>
   <div class="hideDiv">
     <input type="submit" name=btnSubmit value="PROCEED TO THE NEXT STEP &#xf054;" class="blueButton">
   </div>
 </form>

2 个答案:

答案 0 :(得分:2)

使用flatmap返回单个文档数据:

首先导入.flatMap()属性。

{
"IsEncrypted": false,
"Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": "",
    "AzureWebJobsServiceBus": "Endpoint=sb://<namespace>/;SharedAccessKeyName=<keyname>;SharedAccessKey=<key>",
    "AzureWebJobsDocumentDBConnectionString": "mongodb://..."
  }
}

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

mscorlib: Exception while executing function: HttpTriggerSave. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'outputDoc'. Microsoft.Azure.Documents.Client: Value cannot be null.
Parameter name: authKeyOrResourceToken.

然后你可以订阅它,它将返回flatened json而不是数组。

编辑:

你可以这样做直接在你的html中使用它:

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

在html代码中:

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

答案 1 :(得分:0)

这是我提出的解决方案:

//HTML
{{userInfo}}

//TypeScript
this.afs.collection('Occupations',
ref => ref.where('id_user', '==', this.idAuth)  
.limit(1))
.valueChanges()
.flatMap(result => result)
.subscribe(
    v => {
        let y = JSON.parse(JSON.stringify(v));
        this.userInfo = y.duration;                 
    }, 
    (error) => {
             console.log(error);
    }, () => {
        console.log('Finished');
    });