title: Express POST request with Insomnia - req.body returns {}
body: <p>I'm following a video lecture on Express &amp; pg. I am coding along, so all of the code is an exact copy from the video lecture. I am attempting to make a POST request to insert a new user into my users table with Insomnia (I have also tried using the chrome RESTED extension), but keep running into an error saying:</p>
  <pre><code>{
    &quot;error&quot;: {
      &quot;message&quot;: &quot;null value in column \&quot;name\&quot; violates not-null constraint&quot;,
      &quot;status&quot;: 500
    }
  }
  </code></pre>
  <p>However, my request looks like this:</p>
  <pre><code>{
    &quot;name&quot;: &quot;Jellybean&quot;,
    &quot;type&quot;: &quot;admin&quot;
  }
  </code></pre>
  <p>I spoke with a T.A. and he used the same code me, but he did not receive an error. We spoke for about 30 minutes and eventually he said he couldn't figure out what the problem was because my code was working fine on his end and he was using the same request body as me. I thought it might be a problem with Insomnia, but the same problem occurs when I use RESTED chrome extension. Any help would be greatly appreciated.</p>
  <p>Here is my users.js (routes):</p>
  <pre><code>/** Routes for users of pg-intro-demo. */
  
  const express = require(&quot;express&quot;);
  const ExpressError = require(&quot;../expressError&quot;);
  const router = express.Router();
  const db = require(&quot;../db&quot;);
  
  router.get(&quot;/&quot;, async (req, res, next) =&gt; {
      try {
          console.log(req);
          const results = await db.query(`SELECT * FROM users`);
          return res.json({ users: results.rows });
      } catch (e) {
          return next(e);
      }
  });
  
  router.get(&quot;/search&quot;, async (req, res, next) =&gt; {
      try {
          const type = req.query.type;
          const results = await db.query(`SELECT * FROM users WHERE type=$1`, [type]);
          return res.json(results.rows);
      } catch (e) {
          return next(e);
      }
  });
  
  router.post(&quot;/&quot;, async (req, res, next) =&gt; {
      try {
          const { name, type } = req.body;
          const results = await db.query(
              &quot;INSERT INTO users (name, type) VALUES ($1, $2) RETURNING id, name, type&quot;,
              [name, type]
          );
          return res.json(results.rows);
      } catch (e) {
          return next(e);
      }
  });
  
  module.exports = router;
  </code></pre>
  <p>Here is my app.js:</p>
  <pre><code>const express = require(&quot;express&quot;);
  const app = express();
  const ExpressError = require(&quot;./expressError&quot;);
  
  // Parse request bodies for JSON
  app.use(express.json());
  
  const uRoutes = require(&quot;./routes/users&quot;);
  app.use(&quot;/users&quot;, uRoutes);
  
  /** 404 handler */
  
  app.use(function (req, res, next) {
      const err = new ExpressError(&quot;Not Found&quot;, 404);
  
      // pass err to the next middleware
      return next(err);
  });
  
  /** general error handler */
  
  app.use(function (err, req, res, next) {
      // the default status is 500 Internal Server Error
      let status = err.status || 500;
  
      // set the status and alert the user
      return res.status(status).json({
          error: {
              message: err.message,
              status: status,
          },
      });
  });
  
  module.exports = app;
  </code></pre>
  <p>Here is my db.js:</p>
  <pre><code>const { Client } = require(&quot;pg&quot;);
  
  let DB_URI;
  
  // If we're running in test &quot;mode&quot;, use our test db
  // Make sure to create both databases!
  if (process.env.NODE_ENV === &quot;test&quot;) {
    DB_URI = &quot;postgresql:///usersdb_test&quot;;
  } else {
    DB_URI = &quot;postgresql:///usersdb&quot;;
  }
  
  let db = new Client({
    connectionString: DB_URI
  });
  
  db.connect();
  
  module.exports = db;
  </code></pre>
  <p>Here is my data.sql (creates the table &amp; starting data):</p>
  <pre><code>CREATE DATABASE usersdb;
  
  \c usersdb;
  
  DROP TABLE IF EXISTS users;
  
  CREATE TABLE users
  (
    id SERIAL PRIMARY KEY,
    name text NOT NULL,
    type text NOT NULL
  );
  
  INSERT INTO users
    (name, type)
  VALUES
    ('Juanita', 'admin');
  
  INSERT INTO users
    (name, type)
  VALUES
    ('Jenny', 'staff');
  
  INSERT INTO users
    (name, type)
  VALUES
    ('Jeff', 'user');
  
  INSERT INTO users
    (name, type)
  VALUES
    ('Jasmine', 'user');
  
  INSERT INTO users
    (name, type)
  VALUES
    ('James', 'staff');
  
  INSERT INTO users
    (name, type)
  VALUES
    ('Jaimee', 'admin');
  </code></pre>
  <p>Here is my server.js:</p>
  <pre><code>// RUN THIS FILE TO START THE SERVER, NOT APP.JS!
  const app = require('./app');
  
  app.listen(3000, function () {
    console.log(&quot;Server started on 3000&quot;);
  });
  </code></pre>
  <p>Here is my package.json:</p>
  <pre><code>{
      &quot;name&quot;: &quot;pg-intro&quot;,
      &quot;version&quot;: &quot;1.0.0&quot;,
      &quot;description&quot;: &quot;&quot;,
      &quot;main&quot;: &quot;app.js&quot;,
      &quot;scripts&quot;: {
          &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;
      },
      &quot;keywords&quot;: [],
      &quot;author&quot;: &quot;&quot;,
      &quot;license&quot;: &quot;ISC&quot;,
      &quot;dependencies&quot;: {
          &quot;express&quot;: &quot;^4.17.1&quot;,
          &quot;pg&quot;: &quot;^8.5.1&quot;,
          &quot;supertest&quot;: &quot;^4.0.2&quot;
      }
  }
  </code></pre>
  <p>The get routes work perfectly fine, but the post route keeps giving me the error and printing {} when I console.log(req.body).</p>