# 04 - Crafting the Response

In this module, we'll use the `then()` function to return a Promise for our request.

## Overview

It's great that our model and routes are working, but there was a slight hiccup with our last post: If you look at the console, you'll see that the success message actually printed BEFORE the data was inserted into the database. What if the insert had failed though and the data couldn't be entered? For this reason, we need to make sure that the response to the user comes AFTER the insert statement.

## The Code

Go into the `testcontroller.js` file and add the following method. Add it to the bottom of the file, but above the export statement.

```javascript
//STEP 4 - Use this with Postman
router.post('/four', function (req, res) {
  var testData = req.body.testdata.item;
  TestModel
    .create({
      testdata: testData
    })
    .then( //1
      function message() { //2
       res.send("Test 4 went through!");
      }
    );
});
```

## Analysis

Here are the updates that we've made: 1. We call the `then()` method. As you'll read in the the MDN docs, the `then()` method returns a Promise. Hence, we use this asynchronous function to force the message to wait for the insert statement to finish. 2. The callback function will print the success message to the console once `testData` is done running.

## Testing

Let's use Postman to test this:&#x20;

1\. Make sure your server is running.&#x20;

2\. Open Postman.&#x20;

3\. Open a new request.&#x20;

4\. Change the dropdown to POST.&#x20;

5\. Enter the endpoint into the URL: `http://localhost:3000/test/four`.&#x20;

6\. Click on the body tab under the url input field.&#x20;

7\. Choose the `raw` radio button.&#x20;

8\. In the dropdown, choose `JSON (application/json)`.&#x20;

9\. In the empty space, add a JSON object like the one below:

```javascript
{
    "testdata":{
        "item":"step 4"
    }
}
```

10\. Press send.

11\. You should see the following:

![screenshot](/files/-LAU8k5vGmlv64_gmHA1)

12\. Let's also go to Postgres and make sure the data is there. To update the table, you can press the `Execute` button (the lightning bolt).&#x20;

![screenshot](/files/-LAU8k74uQxG72Vq43CH)

## Summary of the Flow

In this module, the following flow is happening:&#x20;

1\. We make a POST request with Postman.&#x20;

2\. `body-parser` breaks the request into JSON.&#x20;

3\. The router sends the request to the `testcontroller`.&#x20;

4\. The controller with the `/four` endpoint is called.&#x20;

5\. The `req.body.testdata.item` is captured in the `testData` variable.&#x20;

6\. We then use the Sequelize `create()` method to create the object to be sent to the DB.&#x20;

7\. The object is sent to Postgres, which stores it.&#x20;

8\. After the data is stored, we fire the `then()` method, which returns a Promise.&#x20;

9\. A method fires a response to Postman.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eleven-fifty-academy.gitbook.io/javascript-152-nodeserver/js_library/node-server/05-model-view-controller/03-controllers/04-crafting-the-response.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
