Ravi Srinivasan
2019-09-12 6b13d515c8a240b3c506166d27574d2917115d8b
Improved error msg handling
3 files modified
52 ■■■■■ changed files
contacts/db/config.js 2 ●●● patch | view | raw | blame | history
contacts/routes/index.js 11 ●●●●● patch | view | raw | blame | history
contacts/views/index.pug 39 ●●●● patch | view | raw | blame | history
contacts/db/config.js
@@ -5,6 +5,6 @@
const pgconn = new Pool({
    connectionString: DB_CONFIG,
    ssl: false,
})
});
  
module.exports = { pgconn }
contacts/routes/index.js
@@ -6,11 +6,14 @@
router.get('/', function(req, res) {
  pgconn.query('SELECT * FROM contacts', function(err,results) {
    if (err) {
      throw err
      console.log(err);
      res.render('index', { error: 'Database connection failure! '+err.stack, contacts: null, title: 'Contact List' });
    }
    let contacts = results.rows;
    console.log(contacts);
    res.render('index', { contacts: contacts, title: 'Contact List' });
    else {
      let contacts = results.rows;
      console.log(contacts);
      res.render('index', { error: null, contacts: contacts, title: 'Contact List' });
    }
  })
});
contacts/views/index.pug
@@ -6,22 +6,25 @@
      h1= title
      hr
      br
      table.table.table-striped.table-bordered.table-hover
        if contacts
          thead
            tr
              th ID
              th First Name
              th Last Name
              th EMail
          tbody
            for contact in contacts
      if error
        .alert.alert-danger #{error}
      else
        table.table.table-striped.table-bordered.table-hover
          if contacts
            thead
              tr
                td= contact.id
                td= contact.firstname
                td= contact.lastname
                td= contact.email
        else
            tr
              td
                | No contacts found
                th ID
                th First Name
                th Last Name
                th EMail
            tbody
              for contact in contacts
                tr
                  td= contact.id
                  td= contact.firstname
                  td= contact.lastname
                  td= contact.email
          else
              tr
                td
                  | No contacts found