<?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: Matthew Cullen</title>
    <description>The latest articles on DEV Community by Matthew Cullen (@3mustard).</description>
    <link>https://dev.to/3mustard</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%2F455293%2F6f09bd30-1cd0-4a32-9779-26a9ebf7c885.jpeg</url>
      <title>DEV Community: Matthew Cullen</title>
      <link>https://dev.to/3mustard</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/3mustard"/>
    <language>en</language>
    <item>
      <title>C# Constructor Overloading Example</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 08 Jun 2021 23:55:00 +0000</pubDate>
      <link>https://dev.to/3mustard/c-constructor-overloading-example-3e3b</link>
      <guid>https://dev.to/3mustard/c-constructor-overloading-example-3e3b</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System;

namespace LESSON2_ConstructorHandsOn
{
    class Program
    {
        static void Main(string[] args)
        {
            Owner momo = new Owner("Momo");
            Cat ownedCat = new Cat("Lennny", momo);
            Cat unownedCat = new Cat("Buddy");
        }
    }

    class Cat
    {
        public string Name { get; set; }
        public Owner Owner { get; set; }

        public Cat(string name)
        {
            Name = name;
            Console.WriteLine($"ADDED CAT: {Name}");
        }

        public Cat(string name, Owner owner)
        {
            Name = name;
            Owner = owner;
            Console.WriteLine($"ADDED CAT: {Name}, OWNER: {Owner.Name}");
        }
    }

    class Owner
    {
        public string Name { get; set; }

        public Owner(string name)
        {
            Name = name;
            Console.WriteLine($"ADDED OWNER: {Name}");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//// OUTPUT
//ADDED OWNER: Momo
//ADDED CAT: Lenny, OWNER: Momo
//ADDED CAT: Buddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>oop</category>
    </item>
    <item>
      <title>Read/Write to Files in C# with errorLogging example</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 08 Jun 2021 00:03:24 +0000</pubDate>
      <link>https://dev.to/3mustard/read-write-to-files-in-c-with-errorlogging-example-3gjp</link>
      <guid>https://dev.to/3mustard/read-write-to-files-in-c-with-errorlogging-example-3gjp</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/3Mustard/cSharpCliErrorLogger"&gt;https://github.com/3Mustard/cSharpCliErrorLogger&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example of how to use ErrorLogger to write one or multiple errors to a file and display them 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;string filePath = System.IO.Path.GetFullPath(@"..\logs.txt");
ErrorLogger eLogger = new ErrorLogger(filePath);

Error err = new Error();
Error err2 = new Error();
Error err3 = new Error();
err.Message = "new error message 1";
err.Date = DateTime.UtcNow.ToString();
err2.Message = "new error message 2";
err2.Date = DateTime.UtcNow.ToString();
err3.Message = "new error message 3";
err3.Date = DateTime.UtcNow.ToString();

List&amp;lt;Error&amp;gt; errors = new List&amp;lt;Error&amp;gt;();
errors.Add(err2);
errors.Add(err3);

eLogger.LogNewError(err);
eLogger.LogAllNewErrors(errors);
eLogger.DisplayErrorsToConsole();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>C# Simple Event Handling Demo</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Thu, 03 Jun 2021 19:43:36 +0000</pubDate>
      <link>https://dev.to/3mustard/c-simple-event-handling-demo-1jn7</link>
      <guid>https://dev.to/3mustard/c-simple-event-handling-demo-1jn7</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/3Mustard/CSharpEventHandlingDemo"&gt;https://github.com/3Mustard/CSharpEventHandlingDemo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>delegates</category>
      <category>events</category>
    </item>
    <item>
      <title>CRUD with C#/SQL in .NET Core</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 09 Feb 2021 21:57:16 +0000</pubDate>
      <link>https://dev.to/3mustard/crud-with-c-sql-in-net-core-40k8</link>
      <guid>https://dev.to/3mustard/crud-with-c-sql-in-net-core-40k8</guid>
      <description>&lt;p&gt;Refer to the repo: &lt;a href="https://github.com/3Mustard/basic-crud-csharp-sql"&gt;HERE&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SETUP:
&lt;/h2&gt;

&lt;h4&gt;
  
  
  ~DataBase
&lt;/h4&gt;

&lt;p&gt;The database for this example is the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TABLE NAME: CrudAppUsers&lt;/li&gt;
&lt;li&gt;Id int primaryKey&lt;/li&gt;
&lt;li&gt;Username varchar(50)&lt;/li&gt;
&lt;li&gt;Password varChar(50)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  ~Data Connection String
&lt;/h4&gt;

&lt;p&gt;To get a data connecting string and access your data base do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In Visual Studio go to View -&amp;gt; Server Explorer.&lt;/li&gt;
&lt;li&gt;In Server Explorer window, Under Data Connections Select your Database. Right Click your Database -&amp;gt; Click Properties.&lt;/li&gt;
&lt;li&gt;In Properties window you will see your Connection String&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  CREATE
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SqlConnection cn = new SqlConnection(@"PUT DATA CONNECTION STRING HERE");
cn.Open();
string Username, Password, Id;
Console.WriteLine("Enter The id and the new username/password of the new user: ");
Id = Console.ReadLine();
Username = Console.ReadLine();
Password = Console.ReadLine();

SqlCommand cmd = new SqlCommand("INSERT INTO CrudAppUsers VALUES (@Id, @Username, @Password)", cn);
cmd.Parameters.AddWithValue("@Id", int.Parse(Id));
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.ExecuteNonQuery();
cn.Close();
Console.WriteLine("Success!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  READ :
&lt;/h3&gt;

&lt;h4&gt;
  
  
  ~Select All
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SqlConnection cn = new SqlConnection(@"PUT DATA CONNECTION STRING HERE");
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM CrudAppUsers", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow row in dt.Rows)
{
  string Id = row["Id"].ToString();
  string Username = row["Username"].ToString();
  string Password = row["Password"].ToString();
  Console.WriteLine($"ID: {Id}, Username: {Username}, Password: {Password}");
  Console.WriteLine("-------------------------------------------------------");
 }

cn.Close();
Console.WriteLine("Success!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  ~Select by ID
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SqlConnection cn = new SqlConnection(@"PUT DATA CONNECTION STRING HERE");
cn.Open();
string Id;
Console.WriteLine("Enter The id of the user you wish to select: ");
Id = Console.ReadLine();
SqlCommand cmd = new SqlCommand("SELECT * FROM CrudAppUsers WHERE Id=@Id", cn);
cmd.Parameters.AddWithValue("Id", int.Parse(Id));
SqlDataAdapter da = new SqlDataAdapter(cmd); // pass select statement to the data adapter
DataTable dt = new DataTable();
da.Fill(dt); // use the data adapter to fill the empty data table
string Username = dt.Rows[0]["Username"].ToString();
string Password = dt.Rows[0]["Password"].ToString();
Console.WriteLine($"ID: {Id}, Username: {Username}, Password: {Password}");
cn.Close();
Console.WriteLine("Success!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  UPDATE
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SqlConnection cn = new SqlConnection(@"PUT DATA CONNECTION STRING HERE");
cn.Open();
string Username, Password, Id;
Console.WriteLine("Enter The id of the user to update and the new username/password: ");
Id = Console.ReadLine();
Username = Console.ReadLine();
Password = Console.ReadLine();
SqlCommand cmd = new SqlCommand("UPDATE CrudAppUsers SET Username=@Username, Password=@Password WHERE Id = @Id", cn);
cmd.Parameters.AddWithValue("@Id", int.Parse(Id));
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.ExecuteNonQuery();
cn.Close();
Console.WriteLine("Success!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  DELETE
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SqlConnection cn = new SqlConnection(@"PUT DATA CONNECTION STRING HERE");
cn.Open();
string Id;
Console.WriteLine("Enter The id of the user you wish to delete: ");
Id = Console.ReadLine();
SqlCommand cmd = new SqlCommand("DELETE CrudAppUsers WHERE Id=@Id", cn);
cmd.Parameters.AddWithValue("@Id", int.Parse(Id));
cmd.ExecuteNonQuery();
cn.Close();
Console.WriteLine("Success!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>csharp</category>
      <category>sql</category>
      <category>dotnet</category>
      <category>database</category>
    </item>
    <item>
      <title>C# Useful Array.Methods()</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Mon, 25 Jan 2021 00:04:55 +0000</pubDate>
      <link>https://dev.to/3mustard/c-useful-array-methods-3n2</link>
      <guid>https://dev.to/3mustard/c-useful-array-methods-3n2</guid>
      <description>&lt;p&gt;Array.IndexOf()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;returns index of item inside array&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array, value to find)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };

var result = Array.IndexOf(colors, "red");
WriteLine(result); // 0 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.Exists()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;returns a boolean if conditions are met&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array, conditions)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };

var result = Array.Exists(colors, color =&amp;gt; color.Contains("green"));
WriteLine(result); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.Find()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;returns the first item to meet a condition&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array, condition)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };

var result = Array.Find(colors, color =&amp;gt; color.Contains("green"));
WriteLine(result); // "green"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.FindLast()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Starts search from the LAST index&lt;/li&gt;
&lt;li&gt;returns the first item to meet a condition&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array, condition)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Array.FindIndex()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;returns the index of the element that meets the condition&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array, condition)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };

var result = Array.FindIndex(colors, color =&amp;gt; color.Contains("green"));
WriteLine(result); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.FindAll()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;returns array of all conditions met&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array, condition)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };

var result = Array.FindAll(colors, color =&amp;gt; color.Contains("r"));

foreach (var color in result) 
{ 
  WriteLine(color); 
}
// "red"
// "green"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.Reverse()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;returns array with all elements in reverse order&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };

var result = Array.Reverse(colors);

foreach (var color in result) 
{ 
  WriteLine(color); 
}
// "blue"
// "green"
// "red"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.Copy()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;copy elements to a new array&lt;/li&gt;
&lt;li&gt;can specify how many elements to copy&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (originalArray, copy, numberOfElementsToCopy)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };
var colors2 = new string[colors.length];

Array.Copy(colors, colors2, 2);

foreach (var color in colors2) 
{ 
  WriteLine(color); 
}
// "red"
// "green"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.Sort()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sort the elements of an array&lt;/li&gt;
&lt;li&gt;&lt;em&gt;params: (array)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };
Array.Sort(colors);

foreach (var color in colors) 
{ 
  WriteLine(color); 
}
// "blue"
// "green"
// "red"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array.BinarySearch()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;search a sorted array&lt;/li&gt;
&lt;li&gt;returns the index of the found item&lt;/li&gt;
&lt;li&gt;returns -1 if nothing is found&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var colors = new[] { "red", "green", "blue" };
Array.Sort(colors); // "blue", "green", "red"

var index = Array.BinarySearch(colors, "green");
if (index == -1)
{
  WriteLine("not found");
}
else
{
  WriteLine(index); // 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Tracking a user's IP address through a hidden form field</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 10 Nov 2020 06:13:53 +0000</pubDate>
      <link>https://dev.to/3mustard/tracking-a-user-s-ip-address-through-a-hidden-form-field-3gl3</link>
      <guid>https://dev.to/3mustard/tracking-a-user-s-ip-address-through-a-hidden-form-field-3gl3</guid>
      <description>&lt;p&gt;The script will make a call to an api that returns the clients IP address and then sets the hidden form field to the returned value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    ...
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;form name="contact"  action="/form_submission_page.html"&amp;gt;
      &amp;lt;input type="text" id="ipFormInput" hidden&amp;gt;
      &amp;lt;input type="submit" value="Send"&amp;gt;
    &amp;lt;/form&amp;gt;
  &amp;lt;/body&amp;gt;

  &amp;lt;script type="application/javascript "&amp;gt;
    const ipFormInput = document.getElementById('ipFormInput');

    fetch('https://api.ipify.org?format=json')
        .then((response) =&amp;gt; { return response.json() })
        .then((json) =&amp;gt; {
            const ip = json.ip;
            ipFormInput.value = ip;
            console.log(ip);
        })
        .catch((err) =&amp;gt; { console.error(`Error getting IP Address: ${err}`) })
  &amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Developer Portfolio</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 10 Nov 2020 04:59:15 +0000</pubDate>
      <link>https://dev.to/3mustard/developer-portfolio-2cd9</link>
      <guid>https://dev.to/3mustard/developer-portfolio-2cd9</guid>
      <description>&lt;p&gt;I made and deployed a portfolio today. It was made with html+css and deployed with netlify. Please check it out and share with anyone who is hiring.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matthewclc.com/"&gt;https://www.matthewclc.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>portfolio</category>
      <category>lookingforwork</category>
    </item>
    <item>
      <title>Using Open Graph Meta Tags</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 10 Nov 2020 04:25:37 +0000</pubDate>
      <link>https://dev.to/3mustard/using-open-graph-meta-tags-30jc</link>
      <guid>https://dev.to/3mustard/using-open-graph-meta-tags-30jc</guid>
      <description>&lt;h1&gt;
  
  
  og:tags
&lt;/h1&gt;

&lt;p&gt;I use these to customize the content of previews when linking my websites on social media posts. See the image below for an example.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bjIyYo13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/toodv2coxfjcjjzj7xq0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bjIyYo13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/toodv2coxfjcjjzj7xq0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
To change the content of these fields just add these meta tags in your Header. There are more but for this example I included the most common og tags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;meta property='og:title' content='Title of the article'/&amp;gt;

&amp;lt;meta property='og:image' content='image url'/&amp;gt;

&amp;lt;meta property='og:description' content='Description that will show in the preview'/&amp;gt;

&amp;lt;meta property='og:url' content='URL of the content'/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To learn more check out &lt;a href="https://ogp.me/"&gt;https://ogp.me/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>opengraphprotocol</category>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>JWT Authentication: MongoDB and Node.js</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 13 Oct 2020 06:18:46 +0000</pubDate>
      <link>https://dev.to/3mustard/jwt-authentication-mongodb-and-node-js-27af</link>
      <guid>https://dev.to/3mustard/jwt-authentication-mongodb-and-node-js-27af</guid>
      <description>&lt;p&gt;Please note the README.MD for setup.&lt;br&gt;
Requires a free MongoDB Atlas account for use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/3Mustard/JWT-auth-with-node-express"&gt;JWT-auth-node-express-mongoDBAtlas&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>jwt</category>
      <category>mernstack</category>
    </item>
    <item>
      <title>MERN Stack Middleware: Check if an :id is a valid Object type</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Tue, 13 Oct 2020 05:05:11 +0000</pubDate>
      <link>https://dev.to/3mustard/mern-stack-middleware-check-for-valid-object-based-on-id-3o69</link>
      <guid>https://dev.to/3mustard/mern-stack-middleware-check-for-valid-object-based-on-id-3o69</guid>
      <description>&lt;p&gt;&lt;em&gt;This middleware was made for MERN stack projects.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When performing a GET request on a route that takes an :id to return a single object from a collect it is important to handle cases where the :id provided does not represent a valid object in our database.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GET  http://localhost:5000/api/post/5f85294e21fcef22fd5b8f78&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here is some middleware we can create to help us do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');

// middleware to check for a valid object id in the url
const checkObjectId = (idToCheck) =&amp;gt; (req, res, next) =&amp;gt; {
  if (!mongoose.Types.ObjectId.isValid(req.params[idToCheck]))
    return res.status(400).json({ msg: 'Invalid ID' });
  next();
};

module.exports = checkObjectId;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can import checkObjectId and use it in any request that takes in an :id and return an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// IMPORTS
const express = require('express');
const router = express.Router();
// MIDDLEWARE
const checkObjectId = require('../../middleware/checkObjectId');
// MODELS
const Post = require('../../models/Post');

// @route    GET api/posts/:id
// @desc     Get post by ID
// @access   Public
router.get(
  // ROUTE
  '/:id',
  // MIDDLEWARE
  checkObjectId('id'),
  // CALLBACK
  async (req, res) =&amp;gt; {
    try {
      const post = await Post.findById(req.params.id);

      if (!post) {
        return res.status(404).json({ msg: 'Post not found' })
      }

      res.json(post);
    } catch (err) {
      console.error(err.message);

      res.status(500).send('Server Error');
    }
  }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>node</category>
      <category>mongodb</category>
      <category>middleware</category>
      <category>mern</category>
    </item>
    <item>
      <title>React/Firebase Image Upload Progress Bar</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Thu, 03 Sep 2020 04:51:56 +0000</pubDate>
      <link>https://dev.to/3mustard/react-firebase-image-upload-3m88</link>
      <guid>https://dev.to/3mustard/react-firebase-image-upload-3m88</guid>
      <description>&lt;p&gt;Technology used: Reactjs, Google Firebase, Semantic-ui-react&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F01bhwrzqr7evbcxwtrr9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F01bhwrzqr7evbcxwtrr9.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  topics covered
&lt;/h2&gt;

&lt;p&gt;-state &lt;br&gt;
-asynchronous requests&lt;br&gt;
-error handling &lt;br&gt;
-file uploads (images in this tutorial)&lt;br&gt;
-custom components from semantic-ui-react to save time&lt;/p&gt;

&lt;p&gt;The goal is to write code that enables a user upload an image file to Google Firebase and display the progress of the upload.&lt;br&gt;
We will be using some custom components from semantic-ui-react to save time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step One ~ Setup a Component
&lt;/h2&gt;

&lt;p&gt;Lets start with a simple class component that includes a form with a field for a file upload and a submit button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { 
  Form, 
  Button, 
} from 'semantic-ui-react';

class ExampleForm extends React.Component {
    state = {
      file: null,
      errors: [],
      uploadState: null
    };

    render () {
      return (
        &amp;lt;Form onSubmit={this.handleSubmit}&amp;gt; 
          &amp;lt;Input
              onChange={this.addFile}
              disabled={uploadState === 'uploading'}
              name="file"
              type="file"
           /&amp;gt;
         &amp;lt;Button&amp;gt;Post&amp;lt;/Button&amp;gt;
        &amp;lt;/Form&amp;gt;
      );
    };
}

export default ExampleForm;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We set a variable in state named 'file' set to null and on our inputs onChange we will build out the addFile()function next to store the file a user selects in 'file'.&lt;br&gt;
I also want to disable the file selection when a file is being uploaded so I will add an uploadState value to state and set the disabled property on our input to be true when uploadState is equal to 'uploading'.&lt;br&gt;
We'll build the handleSubmit() function later as well.&lt;br&gt;
&lt;em&gt;the errors array in state is an optional way to keep track of errors. See the uploadFile function below for use.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step Two ~ addFile()
&lt;/h2&gt;

&lt;p&gt;Lets build out the addFile function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addFile = event =&amp;gt; {
  const file = event.target.files[0];
  if (file) {
    this.setState({ file });
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By passing the event object to our addFile function we can access an array of files attached to it. The image a user selects from clicking our input will be stored in the first index. Lets take this value and store it in our state under the value 'file'.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step three ~ check file type()
&lt;/h2&gt;

&lt;p&gt;Before we send our file lets make sure our user has actually selected an image file.&lt;br&gt;
To check we have the correct file type lets add a new value to state, an isAuthorized function and import the 'mime-type' library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;state = {
  file: null,
  authorizedFileTypes: ['image/jpeg', 'image/png'],
  errors: []
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import mime from 'mime-types';

isAuthorized = filename =&amp;gt; this.state.authorized.includes(mime.lookup(filename))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;isAuthorized will return true if the file type matches one of the strings in our authorizedFileTypes array we added to state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Four ~ uploadFile()
&lt;/h2&gt;

&lt;p&gt;Once we can confirm the file is an image we need a function to upload it to our destination. In this tutorial our destination is Google Firebase. [If you are unsure of how to connect an application to Firebase, I wrote a tutorial on how to do that &lt;a href="https://dev.to/3mustard/setting-up-google-firebase-28ek"&gt;Here.&lt;/a&gt;&lt;br&gt;
I will be importing my unique Firebase config file below, this is where you would import your own unique Firebase config.]&lt;br&gt;
The first step is to import firebase and add a reference of the storage in our Firestore to state. To track our upload lets add a value in state called percentUploaded and set it to 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import firebase from 'path in dir to firebase.js';

// state will now look like this
state ={
  storageRef: firebase.storage().ref(), // ADD THIS LINE
  percentUploaded: 0, // ADD THIS LINE TOO
  uploadTask: null // ADD THIS LINE TOO, SEE BELOW
  file: null,
  uploadState: null,
  authorizedFileTypes: ['image/jpeg', 'image/png'],
  errors: []
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more thing should be added to state and this is important to getting our progress bar to work later on. We need to add a key that will hold a task or changing value that we will attach a listener to. Lets call it uploadTask and set it to null.&lt;/p&gt;

&lt;p&gt;I will show you the completed function then break down what is happening.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// uuidv4() generates a unique id
import uuidv4 from 'uuid/v4';

uploadFile = (file) =&amp;gt; {
    // location in storage you want to create/send file to
    const filePath = `trades/images/${uuidv4()}.jpg`;

    this.setState({
      uploadTask: this.state.storageRef.child(filePath).put(file)
    },
      () =&amp;gt; {
        this.state.uploadTask.on(
          'state_changed', 
          snap =&amp;gt; {
            const percentUploaded = Math.round((snap.bytesTransferred / snap.totalBytes) * 100);
            this.setState({ percentUploaded }); 
        },
          err =&amp;gt; {
            console.error(err);
            this.setState({
              errors: this.state.errors.concat(err),
              uploadState: 'error',
              uploadTask: null
            });
          },
          () =&amp;gt; {
            this.state.uploadTask.snapshot.ref.getDownloadURL().then(downloadUrl =&amp;gt; {
              console.log('this is the image url', downloadUrl);
              this.setState({ uploadState: 'done' })
            })
            .catch(err =&amp;gt; {
              console.error(err);
              this.setState({
                errors: this.state.errors.concat(err),
                uploadState: 'error',
                uploadTask: null
              })
            })
          }
        )
      }
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's break down what is happening.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.setState({
      uploadTask: this.state.storageRef.child(filePath).put(file)
    },
      () =&amp;gt; {
        this.state.uploadTask.on(
          'state_changed', 
          snap =&amp;gt; {
            const percentUploaded = Math.round((snap.bytesTransferred / snap.totalBytes) * 100);
            this.setState({ percentUploaded }); 
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;We first set the value of uploadTask to the return value of .puts(). This function returns a task which will constantly update until it is complete. The task is our files journey to storage.&lt;/li&gt;
&lt;li&gt;Add a listener with .on() to this.state.uploadTask that listens to state changes. Remember this value in state is constantly changing due to .puts()&lt;/li&gt;
&lt;li&gt;We get access to a snapshot of our value in uploadTask each time it changes. By accessing some keys associated with this snap shot we can calculate how much of the file has been transferred with Math.round((snap.bytesTransferred / snap.totalBytes) * 100) and set this value to our percentUploaded value in state.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;err =&amp;gt; {
            console.error(err);
            this.setState({
              errors: this.state.errors.concat(err),
              uploadState: 'error',
              uploadTask: null
            });
          },
          () =&amp;gt; {
            this.state.uploadTask.snapshot.ref.getDownloadURL().then(downloadUrl =&amp;gt; {
              console.log('this is the image url', downloadUrl);
              this.setState({ uploadState: 'done' })
            })
            .catch(err =&amp;gt; {
              console.error(err);
              this.setState({
                errors: this.state.errors.concat(err),
                uploadState: 'error',
                uploadTask: null
              })
            })
          }
        )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the next line checks for errors and if any are found populates the errors array and resets our uploadTask/State. After that we have access to the uploaded images downloadUrl. Now is a good time to set our uploadState to done and clear the file from state. We can then catch any errors again and end our function.&lt;/p&gt;

&lt;p&gt;We have now successfully taken an image from our computer and sent it to Google Firestore. But what about the progress bar?&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Five Progress bar
&lt;/h2&gt;

&lt;p&gt;lets create our own progress bar component and import semantic-ui-react's progress bar into it to customize. We will be passing two values from our first component to the progress bar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { Progress } from 'semantic-ui-react';

const ProgressBar = ({ uploadState, percentUploaded }) =&amp;gt; (
  uploadState === "uploading" &amp;amp;&amp;amp; (
    &amp;lt;Progress 
      className="progress__bar"
      percent={percentUploaded}
      progress
      indicating
      size="medium"
      inverted
    /&amp;gt;
  )
);

export default ProgressBar;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our main component lets import this progressBar and add it into our render function. Don't forget to pass uploadState and percentUploaded down as props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ProgressBar from 'dir of progress bar';

&amp;lt;ProgressBar 
  uploadState={uploadState} 
  percentUploaded={percentUploaded}
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now as our file uploads we will see our progress bar fill up. The only thing left to do is tie it all together in our handleSubmit function and clean up our component's state and listeners.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Six ~ handleSubmit() and componentWillUnmount
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handleSubmit = event =&amp;gt; {
   uploadFile(this.state.file);
  };

componentWillUnmount() {
    if (this.state.uploadTask !== null) {
      this.state.uploadTask.cancel();
      this.setState({ uploadTask: null });
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There you have it, an image upload and a % upload tracker. I hope this helps anyone working with Firebase and React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus error handling
&lt;/h2&gt;

&lt;p&gt;lets make two functions to handle any errors we may encounter.&lt;br&gt;
First give the our file input element a className equal to calling a new function we will setup called handleInputErrors. Pass this function our errors array from state and a 'keyword' to look for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Input
   className={this.handleInputError(errors,'file')}
   onChange={this.addFile}
   disabled={uploadState === 'uploading'}
   fluid
   label="File types: jpg, png"
   name="file"
   type="file"
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handleInputError = (errors, inputName) =&amp;gt; {
    return errors.some(error =&amp;gt; 
      error.message.toLowerCase().includes(inputName)
      )
        ? "error"
        : ""
  }

  displayErrors = errors =&amp;gt; errors.map((error, i) =&amp;gt; &amp;lt;p key={i}&amp;gt;{error.message}&amp;lt;/p&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first function, handleInputError will check if we have an error in our array dealing with 'file' and if so our className will become 'error'. We can style the error class red so that when the className becomes error it will reflect that state to our user. Our last function displayErrors will just map over our errors array and display the message associated with each.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Fix slow compile time in Unreal engine</title>
      <dc:creator>Matthew Cullen</dc:creator>
      <pubDate>Wed, 02 Sep 2020 02:29:46 +0000</pubDate>
      <link>https://dev.to/3mustard/fix-slow-compile-time-in-unreal-engine-53o6</link>
      <guid>https://dev.to/3mustard/fix-slow-compile-time-in-unreal-engine-53o6</guid>
      <description>&lt;p&gt;Unreal engine version: 4.22&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In your code editor go to the file Explorer tab and navigate to
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/UE_4.22/Engine/Binaries/&amp;lt;depending on your system name may be different&amp;gt;Win64/UnrealHeaderTool.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Open UnrealHeaderTool.target&lt;/li&gt;
&lt;li&gt;Add a white space somewhere so you can save the file as if a change took place.&lt;/li&gt;
&lt;li&gt;Compile in Unreal Engine. 
*The first compile after this fix may take awhile but subsequent compiles should be much shorter.&lt;/li&gt;
&lt;/ol&gt;

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