简体   繁体   English

Reactnative:查询和过滤 Firestore 数据库并将单个返回的文档字段值保存在 const/var 中

[英]Reactnative: Query and Filter Firestore Database and save a single returned document field value in a const/var

I have setup a Firebase Firestore Database and would like to filter it for a certain field value in a document.我已经设置了一个 Firebase Firestore 数据库,并希望针对文档中的某个字段值对其进行过滤。 I have a collection called "PRD" with thousands of documents where all contain the same fields.我有一个名为“PRD”的集合,其中包含数千个文档,其中都包含相同的字段。 One of these fields a Document contains is a GTIN Number (String).文档包含的这些字段之一是 GTIN 编号(字符串)。 I am receiving this Number from a Bar Code (called data), and would like to retrieve the Medication Name (called DSCRD, a different Field in all these Documents) using the GTIN Number scanned.我从条形码(称为数据)收到此编号,并希望使用扫描的 GTIN 编号检索药物名称(称为 DSCRD,所有这些文档中的不同字段)。

I am having difficulties with retrieving the Value from the Firebase and the documentation doesn't seem to get me any further.我在从 Firebase 中检索值时遇到困难,而且文档似乎没有进一步帮助我。 I have tried various retrieval methods.我尝试了各种检索方法。 At the moment the code for retrieval looks like this:目前检索代码如下所示:

import { dbh } from "../firebase/config"
import firestore from '@react-native-firebase/firestore'
    dbh.collection('PRD')
    .where('GTIN', '==', data)
    .get()
    .then(documentSnapshot => {

      console.log('MedData',documentSnapshot.data())    
    });

I am unsure how to filter the right medicament using the GTIN provided by the Barcode scanner and then save the specific field value for the description of this medicament to a variable.我不确定如何使用条形码扫描器提供的 GTIN 筛选正确的药物,然后将用于描述该药物的特定字段值保存到变量中。

Firebase is setup correctly since I am able to write whole collections and documents in it. Firebase 设置正确,因为我能够在其中编写整个 collections 和文档。

Here is the database structure, as you can see there is the PRD Collection with all the medicaments and every medicament containing the GTIN and DSCRD Fields:这是数据库结构,如您所见,PRD Collection 包含所有药物以及包含 GTIN 和 DSCRD 字段的每种药物:

The problem with your implementation is that you are trying to call documentSnapshot.data() after querying a collection.您的实现的问题是您在查询集合后尝试调用documentSnapshot.data() This is the syntax you would use if you were fetching a single document.这是您在获取单个文档时将使用的语法。 Your current query will return a list of documents which you need to handle like this:您当前的查询将返回您需要像这样处理的文档列表:

.then(querySnapshot => {
  querySnapshot.forEach(doc => {
    console.log('MedData', doc.data())  
  })  
});

Assuming that the GTIN will fetch one unique document (will it?) then you can just use the only document returned by the query to get the name of the Medication like this:假设 GTIN 将获取一个唯一文档(会吗?),那么您可以只使用查询返回的唯一文档来获取 Medication 的名称,如下所示:

    var medName
    dbh.collection('PRD')
    .where('GTIN', '==', data)
    .get()
    .then(querySnapshot => {
       querySnapshot.forEach(doc => {
          console.log('MedData', doc.data())  
          medName = doc.data().DSCRD
       })  
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM