使用JS映射来自两个Firestore集合的数据 [英] Mapping data from two Firestore collections with js

查看:33
本文介绍了使用JS映射来自两个Firestore集合的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合,分别是: CURRENCY-PAIR Alerts .

I have two collections namely:CURRENCY-PAIR and Alerts.

CURRENCY-PAIR 集合包含以下内容;

The CURRENCY-PAIR collection contains the following;

  • 货币对名称
  • 货币要价
  • 货币出价

警报集合包含以下内容:

  • Alert_id

  • Alert_id

警报状态

如何映射 CURRENCY-PAIR 集合中的货币对名称 Alerts Alert_Status >收集到同时显示两者的列表.

How can i map the Currency-Pair Name from the CURRENCY-PAIR collection and Alert_Status from the Alerts collection to a list showing both.

推荐答案

假设您要从firestore中获取数据集合,则应首先获取该集合的引用:

Say you want to fetch a collection of data from firestore, you should first get the reference of the collection:

const currencyRef = firestore().collection('CURRENCY-PAIR');
const alertRef = firestore().collection('Alert_Status');

然后您可以使用以下引用从Firestore获取数据:

You could then use these references to get the data from firestore:

currencyRef.get()
  .then((doc) => {
    console.log(doc.data());
  });

如您所见,数据采用承诺的形式,您必须解决该承诺.doc.data()是JS对象形式的集合中所有数据的数组.

由于数据是作为保证提供的,因此您可以创建一个异步提取函数,该函数可以解析保证,并将数据放入一个新的数组中,然后返回该数组.也许您可以执行以下操作:

As you can see, the data is in the form of a promise, which you have to resolve. The doc.data() is an array of all the data in your collection, in the form of JS objects.

Since the data comes as a promise, you can create an async fetch function, which resolves the promise, and puts the data in a new array which gets returned. Maybe you can do something like this:

const fetchAllCurrencies = async () => {
  const obj = []; // empty array to put collections in
  const currencyRef = firestore().collection('CURRENCY-PAIR'); // ref
  const snapshot = await currencyRef.get() // resolve promise from firestore
  snapshot.forEach((doc) => { // loop over data
    obj.push({ id: doc.id, ...doc.data() }); // push each collection to array
  });
  return obj; // return array with collection objects 
}

您可以为警报收集创建类似的功能.

我不确定您的意思是什么:如何将CURRENCY-PAIR集合中的Currency-Pair名称和Alerts集合中的Alert_Status映射到同时显示两者的列表."

通过创建上述函数,您可以获取集合js对象的数组.您可以将两个数组结合使用:

You may create a similar function for the alert collection.

I'm not entirely sure about what you mean with: 'How can i map the Currency-Pair Name from the CURRENCY-PAIR collection and Alert_Status from the Alerts collection to a list showing both.'

by creating functions like the one above, you can get arrays of collection js objects. You can combine two arrays with:

const newArray = array1.concat(array2);

这会将两个阵列融为一体.这可能不是您想要的.如果我是你,我会将两个阵列分开.

This will melt two arrays into one. It's probably not what you want to do. If I were you, I'd keep the two arrays separate.

这篇关于使用JS映射来自两个Firestore集合的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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