title: Need some assistance in writing this insert into query, what's an effective way to do it?
body: <p>I am putting together a recipe app but this time instead of the MEAN stack I am using PostgreSQL as the database. Everything goes well until its time for me to write the INSERT INTO and its all downhill from there, I have written this code several different ways and these are the errors I have received:</p>
  
  <pre><code>Server Stareted On Port 3000
  events.js:141
        throw er; // Unhandled 'error' event
        ^
  
  error: null value in column "id" violates not-null constraint
  </code></pre>
  
  <p>tried something else and got:</p>
  
  <pre><code>Server Stareted On Port 3000
  events.js:141
        throw er; // Unhandled 'error' event
        ^
  
  error: syntax error at or near "WHERE"
  </code></pre>
  
  <p>The above was from when I thought it woud work by adding where id = </p>
  
  <pre><code>Server Stareted On Port 3000
  error running query { [error: there is no parameter $1]
    name: 'error',
    length: 87,
    severity: 'ERROR',
    code: '42P02',
    detail: undefined,
    hint: undefined,
    position: '59',
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'parse_expr.c',
    line: '824',
    routine: 'transformParamRef' }
  </code></pre>
  
  <p>Tried yet something else and got:</p>
  
  <pre><code>events.js:141
        throw er; // Unhandled 'error' event
        ^
  
  error: INSERT has more expressions than target columns
  </code></pre>
  
  <p>I am at a loss as to what is going on or how to rearrange my code, I mean I thought it was as straightforward as something is missing with the primary id, but after trying all of the above I am spent quite frankly, don't know where to go from here and yes I have even looked at other posts here.</p>
  
  <p>Here is the code for views/app.js:</p>
  
  <pre><code>var express = require('express'),
      path = require('path'),
      bodyParser = require('body-parser'),
      cons = require('consolidate'),
      dust = require('dustjs-helpers'),
      pg = require('pg'),
      app = express();
  
  // create a config to configure both pooling behavior
  // and client options
  // note: all config is optional and the environment variables
  // will be read if the config is not present
  var config = {
    user: 'aleatoire', //env var: PGUSER
    database: 'recipebookdb', //env var: PGDATABASE
    password: '', //env var: PGPASSWORD
    port: 5432, //env var: PGPORT
    max: 10, // max number of clients in the pool
    idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
  };
  
  
  //this initializes a connection pool
  //it will keep idle connections open for 30 seconds
  //and set a limit of maximum 10 idle clients
  var pool = new pg.Pool(config);
  
  
  // Assign Dust Engine To .dust Files
  app.engine('dust', cons.dust);
  
  // Set Default Ext .dust
  app.set('view engine', 'dust');
  app.set('views', __dirname + '/views');
  
  // Set Public Folder
  app.use(express.static(path.join(__dirname, 'public')));
  
  // Body Parser Middleware
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: false}));
  
  app.get('/', function(req, res){
      // to run a query we can acquire a client from the pool,
      // run a query on the client, and then return the client to the pool
      pool.connect(function(err, client, done) {
        if(err) {
          return console.error('error fetching client from pool', err);
        }
        client.query('SELECT * FROM recipes', function(err, result) {
  
          if(err) {
            return console.error('error running query', err);
          }
          res.render('index', {recipes: result.rows});
          //call `done()` to release the client back to the pool
          done();
  
        });
      });
  
  });
  
  app.post('/add', function(req, res){
      // to run a query we can acquire a client from the pool,
      // run a query on the client, and then return the client to the pool
      pool.connect(function(err, client, done) {
        if(err) {
          return console.error('error fetching client from pool', err);
        }
        client.query('INSERT INTO recipes(name, ingredients, directions) VALUES($1, $2, $3, $4)', [req.body.name, req.body.ingredients, 
          req.body.directions]);
  
          done();
          res.redirect('/');
      });
  });
  
  // Server
  app.listen(3000, function(){
      console.log('Server Stareted On Port 3000');
  });
  </code></pre>
  
  <p>Here is package.json file:</p>
  
  <pre><code>{
    "name": "recipebook",
    "version": "1.0.0",
    "description": "A recipe web app with Nodejs and PostgreSQL",
    "main": "app.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
    },
    "author": "Daniel Cortes",
    "license": "ISC",
    "dependencies": {
      "body-parser": "^1.15.2",
      "consolidate": "*",
      "dust": "*",
      "dustjs-helpers": "*",
      "dustjs-linkedin": "*",
      "express": "*",
      "pg": "*"
    }
  }
  </code></pre>
  
  <p>Here is views/layout.dust:</p>
  
  <pre><code>  &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;RecipeBook&lt;/title&gt;
          &lt;link rel="stylesheet" href="/css/bootstrap.css"&gt;
          &lt;link rel ="stylesheet" href="/css/style.css"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div class="container"&gt;
              &lt;div class="row"&gt;
                  &lt;div class="col-md-offset-2 col-md-7"&gt;
                      {+body /}
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="   crossorigin="anonymous"&gt;&lt;/script&gt;
          &lt;script src="/js/bootstrap.js"&gt;&lt;/script&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Here is views/index.dust:</p>
  
  <pre><code>{&gt;"layout" /}
  
  {&lt;body}
      &lt;button  type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#formModal"&gt;
          Add Recipe
      &lt;/button&gt;
  
      &lt;br /&gt;
  
      {#recipes}
          &lt;div class="well"&gt;
              &lt;h4&gt;{name} 
              &lt;button class="btn btn-default pull-right" data-toggle="collapse" href="#recipe_{id}" aria-expanded="false" aria-controls="recipe_{id}"&gt;
                  &lt;span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"&gt;&lt;/span&gt;
              &lt;/button&gt;&lt;/h4&gt;
  
              &lt;div class="collapse" id="recipe_{id}"&gt;
                &lt;br /&gt;
                &lt;br /&gt;
                &lt;p&gt;&lt;strong&gt;Ingredients: &lt;/strong&gt;{ingredients}&lt;/p&gt;
                &lt;p&gt;&lt;strong&gt;Directions: &lt;/strong&gt;{directions}&lt;/p&gt;
                &lt;br /&gt;
                &lt;hr /&gt;
                &lt;button class="btn btn-default edit-recipe"&gt;
                  &lt;span class="glyphicon glyphicon-edit" aria-hidden="true"&gt;&lt;/span&gt;
                &lt;/button&gt;
                &lt;button class="btn btn-danger delete-recipe"&gt;
                  &lt;span class="glyphicon glyphicon-remove" aria-hidden="true"&gt;&lt;/span&gt;
                &lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      {/recipes}
  
      &lt;!-- Add Form Modal --&gt;
      &lt;div class="modal fade" id="formModal" tabindex="-1" role="dialog" aria-labelledby="formModalLabel"&gt;
        &lt;div class="modal-dialog" role="document"&gt;
          &lt;div class="modal-content"&gt;
              &lt;form method="post" action="/add"&gt;
                &lt;div class="modal-header"&gt;
                  &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
                  &lt;h4 class="modal-title" id="myModalLabel"&gt;Add Recipe&lt;/h4&gt;
                &lt;/div&gt;
                &lt;div class="modal-body"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label&gt;Recipe Name&lt;/label&gt;
                      &lt;input type="text" class="form-control" name="name" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label&gt;Ingredients&lt;/label&gt;
                      &lt;textarea name="ingredients" class="form-control"&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label&gt;Directions&lt;/label&gt;
                      &lt;textarea class="form-control" name="directions"&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="modal-footer"&gt;
                  &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                  &lt;input type="submit" class="btn btn-primary" value="Save" /&gt;
                &lt;/div&gt;
            &lt;/form&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  {/body}
  </code></pre>