SheTech
  • SheTech JS bootcamp
  • Level 1 - awesome tasks
    • Introduction to Awesome Tasks
    • Step 1: HTML
    • Step 2: playing with the DOM
    • Step 3: add a new task
    • Step 4: dynamic list of tasks
    • Step 5: adding new items
    • Step 6: removing items
    • Step 7: add some style
    • Step 8: marking items as done
    • Step 9: saving items in the localstorage
    • Step 10: adding filters and bulk actions
    • Step 11: add counters
    • From Awesome Task to the next big thing!
  • Level 2 - timeline
    • Live Timeline
    • Step 1: basic HTML page
    • Step 2: add util library
    • Step 3: show all posts
    • Step 4: add form
    • Step 5: collect form data
    • Step 6: create a new post
    • Step 7: validating posts
    • Step 8: refactoring
    • Step 9: refresh button
    • Step 10: auto refresh
  • Code Editors
  • Useful resources
  • API Reference
  • Glossary
Powered by GitBook
On this page
  • Create a new post.
  • createPost function

Was this helpful?

  1. Level 2 - timeline

Step 6: create a new post

Create a new post.

Now that we have collected user's input we are ready to create a new post.

Replace

console.log(titleElement, bodyElement);

with

createPost(titleElement, bodyElement);

and add the createPost function

function createPost(title, body) {
  var postObject = { title: title.value, body: body.value };
  fetch(CHAT_URI, {
    method: "POST",
    body: serialize(postObject)
  });
}

createPost function

5.1 – Create a payload object

We create a single object containing both title and body variables. Such object carries all the necessary information (post title and message) to the server.

An object is a collection of properties, and a property is an association between a name (or key) and a value.

Given the object represented below, we refer to title and body as object keys and to "A title" and to "A post" as object values.

{
  "title" : "A title",
  "body"  : "A post"
}

5.2 – POST message to the server

Now let's use the fetch function to send our brand new post to the server.

Then we pass a request configuration object as the second parameter of the fetch function.

{
  method: "POST",
  body: serialize(postObject)
}

This object just tells to the fetch function what we want to send to the server and how.

Let's take a closer look to this object structure:

  • The first property, method: "POST", specifies that we want to POST our paylod to the server (we expect back a confirmation by the server).

  • The second property, body: serialize(postObject) specifies the payload that will be to be sent (in this case the new message).

5.3– Serialize function

If You paid attenction, and we are sure you did ;) , you might have noticed the serialize function .

This is a user defined function that you can find in utils/utils.js and that we report here for convenience:

function serialize(obj) {
  return JSON.stringify(obj);
}

This function takes an object parameter and converts it into its string representation using the JSON.stringify method.

JSON is a specific Javascript notation for objects, arrays, numbers, strings and other Javascript's type. It is often used to share information between the client (our Javascript application) and the server.

What We just did:

We introduced a lot of concepts so it is better to recap:

  • We created an object to store user form's data.

  • We configured and used the fetch function to POST our data to the server.

PreviousStep 5: collect form dataNextStep 7: validating posts

Last updated 4 years ago

Was this helpful?

First we specify the server address (FIREBASE_JSON) as we did in .

What is an object literal?
Step 1
What is HTTP POST method
What are json and JSON.stringify?