<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Patrick Bradshaw</title>
    <description>The latest articles on DEV Community by Patrick Bradshaw (@patrickb).</description>
    <link>https://dev.to/patrickb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F200615%2F47b454cd-283c-4abc-a0cc-9c8120a23ff4.jpg</url>
      <title>DEV Community: Patrick Bradshaw</title>
      <link>https://dev.to/patrickb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patrickb"/>
    <language>en</language>
    <item>
      <title>Firestore cross-reference returns a promise object, can't access document value [help]</title>
      <dc:creator>Patrick Bradshaw</dc:creator>
      <pubDate>Sun, 10 May 2020 08:00:40 +0000</pubDate>
      <link>https://dev.to/patrickb/firestore-cross-reference-returns-a-promise-object-can-t-access-document-value-help-3lh1</link>
      <guid>https://dev.to/patrickb/firestore-cross-reference-returns-a-promise-object-can-t-access-document-value-help-3lh1</guid>
      <description>&lt;p&gt;I posted this on StackOverflow originally, but would welcome any suggestions:&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;My app is built with React and Firebase. There is a feature in the app that allows users to create playlists and fill them with tracks from a catalog. A Playlist has a sub-collection called "Tracks" that stores the track ID's and some other info. When viewing a playlist, I want to loop through the tracks and cross-reference them (like a join in SQL) to the Tracks collection to populate the view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Looks something like this before the return...

// Attempting to write an async/await function
async function getTrackDocument(trackId) {
  const track = await firestore
    .collection("tracks")
    .doc(trackId)
    .get();
  return track;
}

useEffect(() =&amp;gt; {
  const playlistRef = firestore.doc(`playlists/${playlistId}`);
  const playlistTracksRef = firestore.collection(
    `playlists/${playlistId}/tracks`
  );

  playlistRef.get().then(doc =&amp;gt; {
    if (doc.exists) {
      const unsubscribe = playlistTracksRef.onSnapshot(snapshot =&amp;gt; {
        // Snapshot of playlist tracks
        const playlistTracks = snapshot.docs.map(doc =&amp;gt; {
          return {
            id: doc.id,
            ...doc.data(),
            // Getting the track document
            trackRef: getTrackDocument(doc.id).then(doc =&amp;gt; {
              return doc.data();
            })
          };
        });

        setTracks(playlistTracks);
      });

      return () =&amp;gt; unsubscribe();
    }
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;The part where I am trying to cross-reference a Playlist Track ID to the Tracks collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trackRef: getTrackDocument(doc.id).then(doc =&amp;gt; {
   return doc.data();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was hoping that would give me the track information I needed. That part returns a promise object in the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trackRef: Promise
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Object
__proto__: Object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;code&gt;[[PromiseValue]]&lt;/code&gt; has all the values, but I can't get seem to get to where I can use the values. I can log &lt;code&gt;doc.data()&lt;/code&gt; to the console and it shows me the values I need eventually. Is there a way to return those values in-line the way the code is written? I've tried different ways of writing async/await functions, and I also tried to return a Promise.resolve(data), always the same results.&lt;/p&gt;

&lt;p&gt;The closest I've come to the behavior I was hoping for was by creating a function where I could grab the Track and just push it to tracks array in state, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// @param arrayOfTrackIDs -- just track IDs from the playlist
function getTracksThenPopulateArray(arrayOfTrackIDs) {
   arrayOfTrackIDs.forEach(trackID =&amp;gt; {
      firestoreTracks.doc(trackID).get().then(doc =&amp;gt; setTracks(tracks =&amp;gt; [...tracks, track])); 
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This wasn't ideal however because I couldn't incorporate .onSnapshot() to update the view if tracks were removed or added.&lt;/p&gt;

</description>
      <category>help</category>
    </item>
    <item>
      <title>Is there a way to sort the reading list chronologically starting with the oldest?</title>
      <dc:creator>Patrick Bradshaw</dc:creator>
      <pubDate>Sun, 10 Nov 2019 00:34:12 +0000</pubDate>
      <link>https://dev.to/patrickb/is-there-a-way-to-sort-the-reading-list-chronologically-starting-with-the-oldest-2pdd</link>
      <guid>https://dev.to/patrickb/is-there-a-way-to-sort-the-reading-list-chronologically-starting-with-the-oldest-2pdd</guid>
      <description>&lt;p&gt;Is there a way to sort the reading list chronologically starting with the oldest? I have a ton of posts in there and would like to read the oldest one first.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
