Skip to content Skip to sidebar Skip to footer

Mapping Data From Two Firestore Collections With Js

I have two collections namely:CURRENCY-PAIR and Alerts. The CURRENCY-PAIR collection contains the following; Currency-Pair Name Currency-AskPrice Currency-BidPrice The Alerts col

Solution 1:

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');

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

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

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 inconst currencyRef = firestore().collection('CURRENCY-PAIR'); // refconst 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 
}

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.

Post a Comment for "Mapping Data From Two Firestore Collections With Js"