0

My problem is that I can enter data from my input boxes into an SQL table, but the problem is that the text from the table will not show on the page i want to. What works is that the playlist page will show the a href links depending on how many playlists i have made just not the text.

I have left out the database details for security reasons.

playlist.jade

extends layout

block content
  h1= title
  p Welcome to #{title}

   h1 My Playlists
   br
   a(href='/login') Login
   br
   a(href='/users/createPlaylist') Create a new Playlists
   br
   a(href='/users/playlistCreated') test
   br

   for playlist, i in playlists
    a(href='/users/playlistCreated') | #{playlist.text}
    br

users.js

var express = require('express');
var mysql = require('mysql');
var router = express.Router();

var dbConnectionInfo = {
  host : '',
  user : '',
  password : '',
  database : 'audio_collections'
};

router.get('/createPlaylist', function(req, res, next) {
  res.render('new_playlist');
});

router.get('/playlistCreated', function(req, res, next) {
  res.render('name_of_created_playlist');
});

router.get('/songCreated', function(req, res, next) {
  res.render('song_page');
});

router.get('/newSong', function(req, res, next) {
  res.render('new_song');
});

router.post('/newPlaylist', function(req, res, next) {

  var dbConnection = mysql.createConnection(dbConnectionInfo);
  dbConnection.connect();

  dbConnection.on('error', function(err) {
    if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') {
      // Let's just ignore this
      console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring ');
    } else {
      // I really should do something better here
      console.log('Got a DB Error: ', err);
    }
  });

  var playlist = {
    text:  req.body.thePlaylist
  };


  dbConnection.query('INSERT INTO Playlists (playlist_name) VALUES(?)',     [playlist.text], function(err, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if  any)
    if (err) {
      throw err;
    }

    // notice that results.insertId will give you the value of the AI (auto-increment) field
    playlist.id = results.insertId;

    // Going to convert my joke object to a JSON string a print it out to the console
    console.log(JSON.stringify(playlist));

    // Close the connection and make sure you do it BEFORE you redirect
    dbConnection.end();

    res.redirect('/');
  });

  router.post('/newSongAdded', function(req, res, next) {

  var dbConnection = mysql.createConnection(dbConnectionInfo);
  dbConnection.connect();

  dbConnection.on('error', function(err) {
    if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') {
      // Let's just ignore this
      console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring ');
    } else {
      // I really should do something better here
      console.log('Got a DB Error: ', err);
    }
  });

  var song = {
    text: req.body.theSong,
    url: req.body.theSongURL
  };

  dbConnection.query('INSERT INTO Songs (song_name, song_url) VALUES(?,?)',[song.text, song.url], function(err, results,fields) {
    // error will be an Error if one occurred during the query
    // results will contain the results of the query
    // fields will contain information about the returned results fields (if any)
    if (err) {
      throw err;
    }

    // notice that results.insertId will give you the value of the AI (auto-increment) field
    song.id = results.insertId;

    // Going to convert my joke object to a JSON string a print it out to the console
    console.log(JSON.stringify(song));

    // Close the connection and make sure you do it BEFORE you redirect
    dbConnection.end();


    res.redirect('/');
  });

 });
});

module.exports = router;

index.js

var express = require('express');
var mysql = require('mysql');
var router = express.Router();

var dbConnectionInfo = {
  host : '',
  user : '',
  password : '',
  database : 'audio_collections'
};

/* GET home page. */
router.get('/login', function(req, res, next) {
  res.render('login');
});

router.post('/login', function(req, res, next) {
  var username = req.body.username;

  username = username.trim();

  if (username.length == 0) {
    res.redirect('/login');
  }
  else {
    req.session.username = username;
    res.redirect('/');
  }
});

router.get('/', function(req, res, next) {
  var dbConnection = mysql.createConnection(dbConnectionInfo);
  dbConnection.connect();

  dbConnection.on('error', function(err) {
    if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') {
      // Let's just ignore this
      console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring ');
    } else {
      // I really should do something better here
      console.log('Got a DB Error: ', err);
    }
  });

  dbConnection.query('SELECT * FROM Playlists', function(err, results, fields){
    if (err) {
      throw err;
    }

    var allPlaylists = new Array();

    for (var i = 0; i < results.length; i++) {
      var playlist = {
        id: results[i].id,
        text: results[i].text
      };

      console.log(JSON.stringify(playlist));

      allPlaylists.push(playlist);
    }

    dbConnection.end();

    res.render('playlists', {playlists: allPlaylists});
  });

  router.get('/users/playlistCreated', function(req, res, next) {
    var dbConnection = mysql.createConnection(dbConnectionInfo);
    dbConnection.connect();

    dbConnection.on('error', function(err) {
      if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') {
        // Let's just ignore this
        console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring ');
      } else {
        // I really should do something better here
        console.log('Got a DB Error: ', err);
      }
    });

    dbConnection.query('SELECT * FROM Songs', function(err, results, fields){
      if (err) {
        throw err;
      }

      var allSongs = new Array();

      for (var i = 0; i < results.length; i++) {
        var song = {};
        song.id = results[i].id;
        song.text = results[i].text;
        song.url = results[i].url;

        console.log(JSON.stringify(song));

        allSongs.push(song);
      }

      dbConnection.end();

      res.render('name_of_created_playlist', {songs: allSongs});
    });
  });
});

module.exports = router;

new_playlist.jade

extends layout

block content

    form(method='POST', action='/users/newPlaylist')
      div
        label(for='thePlaylist') Name of Playlist
        div
          textarea(type='text', name='thePlaylist')
          br
          input(type='submit', name='Add new Playlist')

    a(href='/') Cancel

Here is the database and table setups

database and table setup

I would really appreciate the help and I have been stuck on this for a week now.

1 Answers1

0

Got it fixed I didnt need the for loops inside my array

Only needed var allPlaylist = results; Since results is an array already