Search code examples
node.jsexpressherokupostmanheroku-postgres

Testing Express POST on Heroku with Postman


Problem: When I use Postman to test a user creation POST endpoint, either of the following errors occur.

  • "Error error: column "test2" does not exist
  • "Cannot Post "

Considerations:

  • I am able to successfully perform GET requests using Postman

  • Postman seems to default parameter entry to URI?paramName=paramValue&...&paramNameN=paramvalueN

  • Most of the express documentation I see displays param passing as URI/paramValue/.../paramValueN

  • I have tried both notations but still can't get a successful call

Postman URIs attempted

[URI]/api/v1/users/test2@gmail.com/test/test

  • Error: "Error error: column "test2" does not exist

[URI]/api/v1/users?email=test2@gmail.com&firstName=test&lastName=test

  • Error: "Cannot Post "

Code:

app.post('/api/v1/users/:email/:firstName/:lastName', async(req, res) => {
try {
    const client =  await pool.connect();
    const result =  await client.query(
        `INSERT INTO users(id, email, first_name, last_name, created_date, last_session) VALUES (DEFAULT, ${req.params.email}, ${req.params.firstName}, ${req.params.lastName}, 'now()', 'now()') RETURNING *;`);
    const results = { 'results': (result) ? result.rows : null};
    res.json(results);
    client.release();
  } catch (err) {
    console.error(err);
    res.send("Error " + err);
  }})

Stack: Node, Express, Postgres, Heroku (hobby plan), Postman


Solution

  • The issue turned out to be with the parsing of the params passed into the URL. I instead performed the following:

    • Changed code to accept the body of the request using bodyParser
    • Storing params from the request in a local variable
    • Including adding single quotes around the ${] --> '${}'

    I am now able to write to the DB with the Body's contents.

    const express = require('express');
    const app = express();
    app.use(express.json());
    const { Pool } = require('pg');
    const bodyParser = require('body-parser');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));
    const User = require('./userClass.js');
    
    const pool = new Pool({
      connectionString: process.env.DATABASE_URL,
      ssl: {
        rejectUnauthorized: false
      }
    });
    
    /******* POST *******/
    app.post('/api/v1/users', async(req, res) => {
        let newUser = new User(req.body.email, req.body.firstName, req.body.lastName);
        try {
            const client =  await pool.connect();
            const result =  await client.query(`INSERT INTO users(id, email, first_name, last_name, created_date, last_session) VALUES (DEFAULT, '${newUser.email}', '${newUser.firstName}', '${newUser.lastName}', 'now()', 'now()') RETURNING *;`);
            const results = { 'results': (result) ? result.rows : null};
            res.json(results);
            client.release();
          } catch (err) {
            console.error(err);
            res.send("Error " + err);
          }
    })