<?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: Alex Adamov</title>
    <description>The latest articles on DEV Community by Alex Adamov (@alexadamov).</description>
    <link>https://dev.to/alexadamov</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%2F780382%2Fc77ba9ad-9991-415c-9615-f851c3467862.png</url>
      <title>DEV Community: Alex Adamov</title>
      <link>https://dev.to/alexadamov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexadamov"/>
    <language>en</language>
    <item>
      <title>You should use Firebase as the backend for your first app.</title>
      <dc:creator>Alex Adamov</dc:creator>
      <pubDate>Sat, 19 Mar 2022 14:48:33 +0000</pubDate>
      <link>https://dev.to/alexadamov/you-should-use-firebase-as-the-backend-for-your-first-app-4ai5</link>
      <guid>https://dev.to/alexadamov/you-should-use-firebase-as-the-backend-for-your-first-app-4ai5</guid>
      <description>&lt;p&gt;The backend is an essential part of an application. It is where you save your data, execute some business logic on it and send it back to your frontend to display to the user.&lt;/p&gt;

&lt;p&gt;When building your first app, if you set up a backend that is &lt;strong&gt;too complex&lt;/strong&gt;, you may encounter problems that are &lt;strong&gt;difficult to solve&lt;/strong&gt; and you may &lt;strong&gt;lose motivation&lt;/strong&gt; to finish it. Firebase is a great choice to avoid this. Here is why and what I have learned using it this week.&lt;/p&gt;

&lt;p&gt;I’m building &lt;a href="https://121convo.com/"&gt;121convo&lt;/a&gt;, a tool to help online community managers or online course managers automatically match and schedule one-to-one conversations between the members who wish to do so.&lt;/p&gt;

&lt;p&gt;Firebase has an &lt;strong&gt;easy-to-use database&lt;/strong&gt; called Firestore. It is a NoSQL database. This means your data is not stored in tables with columns and rows but are simply a collection of documents with a simple structure of label of data: data item(s). This gives the flexibility to tailor the data structure how you want and change it later on in the project if you want.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connecting your app to Firebase and its database
&lt;/h3&gt;

&lt;p&gt;Working with Firestore in your application is &lt;strong&gt;dead simple&lt;/strong&gt;. To connect your application to your firebase backend, in ReactJS you simply need to install a firebase package and add a few lines of code with parameters specific to your project in a separate file in your application.&lt;/p&gt;

&lt;p&gt;These few lines connect your app to Firebase and you have &lt;strong&gt;access to services like authentication and database functionality such as write, update and delete.&lt;/strong&gt; He is an example in ReactJS where I import my database, db, and the functions I need. Then I simply collect a database reference for the document (here the document identifier is ‘userId’) in the collection of documents called ‘users’.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { db } from "../firebase.config"
import { doc, getDoc, updateDoc } from "firebase/firestore"

// Get community ID of user
const userDocRef = doc(db, 'users', userId)
const userDocSnap = await getDoc(userDocRef)
const communityId = userDocSnap.data().communityId
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Firebase Cloud Functions
&lt;/h3&gt;

&lt;p&gt;For data manipulation that is more complex than database interaction, for example interacting with a third-party API, you would use a Firebase Cloud Function.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;backend code that is saved in the cloud&lt;/strong&gt; and triggered in response to events (e.g. new record in your database) or an HTTP request (a request you can send from your frontend, for example when a user clicks on a button).&lt;/p&gt;

&lt;p&gt;I used a cloud function to trigger sending opt-in emails to community members. For now, I have it linked to a button that the community manager who signed up to 121convo can control.&lt;/p&gt;

&lt;p&gt;The cloud function connects to the SendGrid API, a popular batch email sending service, passes some variables such as who the email should be sent to, which email template to use (previously created and saved in SendGrid), and some variables to fill placeholders in the email template such as the name of the community to which the member belongs.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://blowstack.com/blog/how-to-send-email-using-firebase-functions"&gt;page&lt;/a&gt; provides the code to do this step in 3 different ways. I picked Option 1: Cloud email service. It is very simple to use the function once it is deployed.&lt;/p&gt;

&lt;p&gt;It is basically a URL to which you send a Get HTTP request from your application. The request includes in the body the parameters which you wish to pass to the function. In the example below I am using axios, a JS package to send the request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const triggerEmail = async () =&amp;gt; {

    const URL = 
    "https://us-central1-&amp;lt;projectid&amp;gt;    
    .cloudfunctions.net/&amp;lt;yourfunctionname&amp;gt;"

    const res = await axios.get(URL, {
      params: {
        to: 'recepient@email.com',
        subject: 'test email from React app',
        first_name: 'Bob',
      }
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, Firebase is a backend solution that is &lt;strong&gt;approachable for beginner builders like me&lt;/strong&gt; with clear documentation and plenty of online resources to copy or get pointers from.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>serverless</category>
    </item>
    <item>
      <title>Getting comfortable with promises in JavaScript</title>
      <dc:creator>Alex Adamov</dc:creator>
      <pubDate>Fri, 11 Mar 2022 09:04:29 +0000</pubDate>
      <link>https://dev.to/alexadamov/getting-comfortable-with-promises-in-javascript-507p</link>
      <guid>https://dev.to/alexadamov/getting-comfortable-with-promises-in-javascript-507p</guid>
      <description>&lt;p&gt;Promises are difficult to understand for people coming from Python. &lt;/p&gt;

&lt;p&gt;I am currently building an application that will have users upload a CSV file containing a list of emails of members of an online community. I used a popular library, Papa parse, to parse the CSV data. &lt;/p&gt;

&lt;p&gt;However, I got stuck for a couple of hours. &lt;/p&gt;

&lt;p&gt;While I could read the file and output it in the console, I could not assign the data to a variable and transfer it to a database. The trick happened in understanding that the library uses asynchronous functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous functions
&lt;/h2&gt;

&lt;p&gt;These are functions that do not execute completely in the sequence of the code. Instead, they return a promise that will do something. This promise may take time to complete and in JavaScript the rest of the code that is not dependent on it continues to execute.  &lt;/p&gt;

&lt;p&gt;This means that any value that is returned by a promise can only be accessed after the code in the scope has finished running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('beginning')
myPromise.then(value =&amp;gt; {console.log('Value inside promise:', value)})
console.log('finish')

/* Output logs:
beginning
finish
Value inside promise: myValue
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another pitfall is that you cannot assign 'value' to a variable and hope to use that value in the rest of your code that is in the same scope of execution as the promise declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let globalVariable
myPromise.then(value =&amp;gt; {
  globalVariable = value
})

useValue(globalVariable) // incorrect: does not work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because the function useValue is being executed before value is returned by myPromise and assigned to globalVariable.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle promises
&lt;/h2&gt;

&lt;p&gt;You basically have to wait for the promise to execute. Include the code that uses the result of the promise inside the .then syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myPromise.then(value =&amp;gt; {
  useValue(value) // works! waits for value
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use an async function with the await keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getValue () {
  // ...
  return value
}

async function main () {
  const value = await getValue() // works! waits for value
  useValue(value)
}

main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or finally, you can assign to a variable but only use the value in code that runs after. For example, code that runs after an event is triggered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let globalVariable

myPromise.then(value =&amp;gt; {
  globalVariable = value
})


myButton.addEventListener('click', event =&amp;gt; {
  if (globalVariable) {
    useValue(globalVariable) 
   // correct but only when click happens after then()
  } else {
    console.log('globalVariable is not yet available')
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key lesson is to be careful with async functions and promises and think about the sequence in which your code will run. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
